展开全部
解释写32313133353236313431303231363533e58685e5aeb931333365633938在注释里面:public class Finallypractice {
public static void main(String[] args) {
ArrayList a = new ArrayList();
a.add(0, "zero");
a.add(1, "one");
a.add(2, "two");
a.add(3, "three");
printAl(a); // 此时a的元素是:zero one two three
if (a.contains("three")) {
a.add("four"); // 此时a的元素是:zero one two three four
}
// remove(int index):移除此列表中指定位置上的元素(a中的索引为2的元素是:two)
a.remove(2);
printAl(a); // 此时a的元素是:zero, one, three, fours
// indexOf: 返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1
if (a.indexOf("four") != 4) {
a.add(4, "4.2"); // 此时a的元素是:zero one three four 4.2
System.out.println(a.indexOf("four")); // 此列表中首次出现four的索引是3(从0开始)
}
printAl(a);
if (a.contains("two")) {
a.add("2.2");
}
printAl(a);
}
public static void printAl(ArrayList al) {
for (String element : al) {
System.out.print(element + " ");
}
System.out.println(" ");
}
}