Python如何将以下代码所有输出的元素重置为“你好”。public class NoteBook { String a = ""; ArrayList<String> lst = new ArrayList<>(); public void addlist(String a) { lst.add(a); } public int getSize(){ return lst.size(); } public String[] list(){ String[] str = new String[lst.size()]; lst.toArray(str); return str; } public static void main(String[] args) { NoteBook nb = new NoteBook(); nb.addlist("张三"); nb.addlist("李四"); nb.addlist("王二麻子"); System.out.println(nb.getSize()); for (String k:nb.list()){ System.out.println(k); } } }
时间: 2024-01-29 12:02:47 浏览: 59
这段代码是Java代码,而不是Python代码。要将输出的元素重置为“你好”,需要在每个addlist()方法中添加代码,将传入的字符串参数改为“你好”。修改后的代码如下:
```java
public class NoteBook {
String a = "";
ArrayList<String> lst = new ArrayList<>();
public void addlist(String a) {
lst.add("你好");
}
public int getSize(){
return lst.size();
}
public String[] list(){
String[] str = new String[lst.size()];
lst.toArray(str);
return str;
}
public static void main(String[] args) {
NoteBook nb = new NoteBook();
nb.addlist("张三");
nb.addlist("李四");
nb.addlist("王二麻子");
System.out.println(nb.getSize());
for (String k:nb.list()){
System.out.println(k);
}
}
}
```
阅读全文