1.List接口提供的适合于自身的常用方法均与索引有关,这是因为List集合为列表类型,以线性方式存储对象,,可以通过对象的索引操作对象。
List接口的常用实现类有ArrayList和LinkedList,在使用List集合时,通常情况下声明为List类型,实例化时根据实际情况的需要,实例化为
ArrayList或LinkedList,例如:List<String>l=newArrayList<String>();//利用ArrayList类实例化List集合List<String>l2=newLinkedList<String>();
//利用LinkedList类实例化List集合
2.add(intindex,Objectobj)方法和set(intindex,Objectobj)方法的区别在使用List集合时需要注意区分add(intindex,Objectobj)方法和set(intindex,Objectobj)方法,
前者是向指定索引位置添加对象,而后者是修改指定索引位置的对象
3.indexOf(Objectobj)方法和lastIndexOf(Objectobj)方法的区别在使用List集合时需要注意区分indexOf(Objectobj)方法和lastIndexOf(Objectobj)方法,
前者是获得指定对象的最小的索引位置,而后者是获得指定对象的最大的索引位置,前提条件是指定的对象在List集合中具有重复的对象,否则如果在List集合中
有且仅有一个指定的对象,则通过这两个方法获得的索引位置是相同的
4 subList(intfromIndex,inttoIndex)方法在使用subList(intfromIndex,inttoIndex)方法截取现有List集合中的部分对象生成新的List集合时,需要注意的是,
新生成的集合中包含起始索引位置代表的对象,但是不包含终止索引位置代表的对象
拓展:
遍历arrayList的四种方法:
package com.test;
import java.util.ArrayList;import java.util.Iterator;import java.util.List;
public class ArrayListDemo { publicstatic void main(String args[]){List<String> list = newArrayList<String>();list.add("luojiahui");list.add("luojiafeng");
//方法1Iterator it1 = list.iterator();while(it1.hasNext()){System.out.println(it1.next());}
//方法2for(Iterator it2 = list.iterator();it2.hasNext();){System.out.println(it2.next());}
//方法3for(String tmp:list){System.out.println(tmp);}
//方法4for(int i = 0;i < list.size(); i ++){System.out.println(list.get(i));}
}}
用最少的悔恨面对过去