package step5; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class Genericity { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<String> stringArrayList = new ArrayList<>(); while (scanner.hasNext()){ stringArrayList.add(scanner.next()); } // ---------------------Begin------------------------ // ---------------------End------------------------ } } 根据提示,在右侧编辑器补充代码,完成一下任务: 1.添加String类型内容为educode的元素 2.遍历输出ArrayList
时间: 2023-08-15 15:13:34 浏览: 151
java代码-import java.util.Scanner;
5星 · 资源好评率100%
可以在以下注释之间添加代码:
```java
// ---------------------Begin------------------------
stringArrayList.add("educode");
// ---------------------End------------------------
```
完整代码如下:
```java
package step5;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Genericity {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> stringArrayList = new ArrayList<>();
while (scanner.hasNext()) {
stringArrayList.add(scanner.next());
}
// ---------------------Begin------------------------
stringArrayList.add("educode");
// ---------------------End------------------------
Iterator<String> iterator = stringArrayList.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
```
运行程序后,输入任意字符串,最后会输出所有输入的字符串,包括添加的"educode"。
阅读全文