运行结果如下:
1. LIST IS: [Uno, Due, java.awt.Point[x=3,y=3], 456, c, Tre, java.awt.Point[x=3,y=3]]
2. Element at position 2 java.awt.Point[x=3,y=3]
3. Uno Due 456 Tre
4. LIST IS: [Uno, Due, 456, c, Tre, java.awt.Point[x=3,y=3]]
解释:
1.定义了方法createArrayList() :该方法中创建了一个ArrayList实例,对其复制(依次地添加元素),并返回该实例;
2.System.out.println( "1. LIST IS: " + myList ),打印“1.LIST:”以及ArrayList类型的myList实例;
3.System.out.println("2. Element at position 2 " + myList.get(2));
打印myList中下标为2的元素;
4.int pos2 = myList.indexOf( new Point(3,3) );
if( pos2 > -1 ) {
myList.remove(pos2);
}
Point item3 = new Point(3, 3);
myList.add(1, item3);
myList.remove(1);
获取元素new Point(3,3)的下标,若存在,则将其删除,并在myList下标为1的位置中插入元素item3;在删除下标为1的元素。
5.for( int i=0; i
if (item.length() == 3 ) {
System.out.print(item.toString() + " ");
}
}
遍历myList,并打印出其长度为3的元素;
6.System.out.println("4. LIST IS: " + myList );
打印整个myList
1. LIST IS: [Uno, Due, java.awt.Point[x=3,y=3], 456, c, Tre, java.awt.Point[x=3,y=3]]
2. Element at position 2 java.awt.Point[x=3,y=3]
3. Uno Due 456 Tre
4. LIST IS: [Uno, Due, 456, c, Tre, java.awt.Point[x=3,y=3]]
1. LIST IS: [Uno, Due, java.awt.Point[x=3,y=3], 456, c, Tre, java.awt.Point[x=3,y=3]]
2. Element at position 2 java.awt.Point[x=3,y=3]
3. Uno Due 456 Tre
4. LIST IS: [Uno, Due, 456, c, Tre, java.awt.Point[x=3,y=3]]
2. Element at position 2 java.awt.Point[x=3,y=3]
3. Uno Due 456 Tre
1. LIST IS: [Uno, Due, java.awt.Point[x=3,y=3], 456, c, Tre, java.awt.Point[x=3,y=3]]
输出整个列表。
2. Element at position 2 java.awt.Point[x=3,y=3]
返回此列表中索引为2的元素.也就是列表中的第3个元素。而第三个元素就是
java.awt.Point[x=3,y=3]
3. Uno Due 456 Tre
输出列表中字符串长度为3的字符串
4. LIST IS: [Uno, Due, 456, c, Tre, java.awt.Point[x=3,y=3]]
因为 int pos2 = myList.indexOf( new Point(3,3) );
if( pos2 > -1 ) {
myList.remove(pos2);
}
把第三个元素给removed掉了。所以结果就这样了。