定义集合对象,并向该集合中添加对象,使用set( )方法修改集合中的元素,使用add( )方法向集合中添加元素。

1,ArrayList类

  ArrayList 类实现了List 接口,由ArrayList 类实现的List 集合采用数组结构保存对象。数组结构的优点是便于对集合进行快速的随机访问,如果经常需要根据索引位置访问集合中的对象,使用由 ArrayList 类实现的List集合的效率较高。数组结构的缺点是向指定索引位置插入对象和删除指定索引位置对象的速度较慢,如果经常需要向 List 集合的指定索引位置插入对象,或者删除 List 集合的指定索引位置的对象,使用由 ArrayList类实现的 List 集合的效率则较低,并且插入或删除对象的索引位置越小效率越低。原因是当向指定的索引位置插入对象时,会同时将指定索引位置及之后的所有对象相应的向后移动一位。

【例】   定义集合对象,并向该集合中添加对象,使用set( )方法修改集合中的元素,使用add( )方法向集合中添加元素。

public classInsertList {

    public static void main(String[] args) {

         String a = "A", b ="B", c = "C", d = "D", e = "E";   //定义要插入集合的字符串对象

         List<String> list = newArrayList<String>();  //创建List集合

          list.add(a);                  //向集合中添加元素

          list.add(e); 

          list.add(d); 

         Iterator<String> fristIterator =list.iterator();

                    //  创建集合的迭代器

         System.out.println("修改前集合中的元素是:" +"\n");  //  输出信息

         while (fristIterator.hasNext()) {//  遍历集合中的元素

             System.out.print(fristIterator.next() + " ");

         }

          list.set(1, b);                //  将索引位置为1的对象e修改为对象b

         Iterator<String> it =list.iterator();  //  创建将集合对象修改之后的迭代器对象

         System.out.println();

         System.out.println("使用set()方法修改后集合中的元素是:" +"\n");

         while (it.hasNext()) {              //  循环获取集合中的元素

              System.out.print(it.next() +" ");

         }

          list.add(2, c);                //  将对象c添加到索引位置为2的位置

         Iterator<String> it2 =list.iterator();

         System.out.println();

         System.out.println("使用add()方法添加集合中的元素是:" +"\n");

         while (it2.hasNext()) {              //  循环获取集合中的元素

              System.out.print(it2.next() +" ");

         }

    }

}

List集合中可以包含重复的对象,若要获取重复对象第一次出现的索引位置可以使用方法indexOf(),如果要获取重复对象最后一次出现的索引位置可以使用 lastIndexOf()方法。使用indexOf() lastIndexOf()方法时,如果指定的对象在List 集合中只有一个,则通过这两个方法获得的索引位置是相同的。

【例】  使用add()方法向集合中添加元素。

public classIndexList {

    public static void main(String[] args) {

                   //要添加到集合中的对象

         String orange = "orange",pear = "pear", banana = "banana", 

                      pineapple ="pineapple", apple = "apple";

                  //创建List集合对象

         List<String> list = newArrayList<String>();

          list.add(orange);    //索引位置为  0

          list.add(apple);    //索引位置为  1

          list.add(pear);            //索引位置为  2

          list.add(apple);         //索引位置为  3

          list.add(banana);        //索引位置为  4

          list.add(apple);         //索引位置为  5

          list.add(pineapple);    //索引位置为  6

         System.out.println("元素apple第一次出现

                              的索引位置为:"+list.indexOf(apple)); 

         System.out.println("元素apple最后一次出现

                              的索引位置为:"+list.lastIndexOf(apple)); 

         System.out.println("元素pear第一次出现的

                             索引位置为:"+list.indexOf(pear)); 

         System.out.println("元素pear最后一次出现的

                             索引位置为:"+list.lastIndexOf(pear)); 

    }

}