JAVA 习题, 高手进

来源:百度知道 编辑:UC知道 时间:2024/06/13 23:54:37
Q6: Complete the output when the ex06() method is executed:

private void ex06() {
ArrayList myList = createArrayList();
System.out.println( "1. LIST IS: " + myList );
Object item1 = myList.get(0);
System.out.println("2. Element at position 2 " + myList.get(2));
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);

System.out.print("3. ");
for( int i=0; i<myList.size(); i++ ) {
String item = (myList.get(i)).toString();
if (item.length() == 3 ) {
System.out.print(item.toString() + " ");
}
}
System.out.println();
System.out.println("4. LIST IS: " + myList );
}
private ArrayList createArrayList() {
ArrayList 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.定义了方法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<myList.size(); i++ ) {
  String item = (myList.get(i)).toString();
  if (item.length() == 3 ) {
  System.out.prin