全方位剖析List

来源:互联网 发布:模拟动物的游戏知乎 编辑:程序博客网 时间:2024/06/06 04:03

在项目中的实战,请见博客“项目实战



数据存储

数组:基本数据类型和引用数据类型

集合:引用数据类型

1.add(Object obj),addAll(Collection coll),size(),clear(),isEmpty()

2.remove(Object obj),removeAll(Collectioncoll),retainAll(Collection coll),equals(object obj),contains(Objectobj),containsAll(Collection coll),hashCode都和对象的所对应的类的equals方法有关,和集合有关的必须重写equals方法

3.iteraor(),toArray()

 

List集合里添加了一些根据索引来操作集合元素的方法

List(常用)

不常用的:

IntindexOf(Object obj)返回obj在集合中首次出现的位置,没有返回-1

IntlastIndexOf(Object obj)最后一次出现的位置,没有返回-1

ListsubList(int fromIndex,int toIndex)返回从fromIndex到toIndex结束左闭右开的的子list

List常用的方法

增add(intindex)或add(Object obj)

删remove(intindex)或remove(Object obj)

改set(intindex,Object obj)

查get(intindex)

长度size()


package TestContainer;import static org.junit.Assert.*;import java.util.ArrayList;import java.util.List;import org.junit.Test;public class TestList {/*void add(int index,Object ele)Boolean addAll(int Index,Collection eles)Object get(int index)Object remove(int index)Object set(int index,Object ele)不常用的:Int indexOf(Object obj)返回obj在集合中首次出现的位置,没有返回-1Int lastIndexOf(Object obj)最后一次出现的位置,没有返回-1List subList(int fromIndex,int toIndex)返回从fromIndex到toIndex结束左闭右开的的子listList 常用的方法增add(int index)或add(Object obj)删remove(int index)或remove(Object obj)改set(int index,Object obj)查get(int index)长度size()*/@Testpublic void testList1(){List list =new ArrayList();list.add(123);list.add(456);list.add(new String("AA"));list.add(123);list.add("GG");//System.out.println("the first index is :"+list.indexOf(123));//System.out.println("the first index is :"+list.indexOf("hadi"));//System.out.println("the last index is :"+list.lastIndexOf(123));System.out.println(list.subList(0,4));//System.out.println(list.subList(0, 10));//System.out.println(list.subList(10, 20));}////ArrayList,List主要实现类,动态数组//@Test//public void testList2() {//List list =new ArrayList();//list.add(123);//list.add(456);//list.add(new String("AA"));//list.add("GG");//System.out.println(list);//list.add(0, 555);//System.out.println(list);//System.out.println(list.get(1));//list.remove(0);//System.out.println(list.get(0));//list.set(0, "hadi");//System.out.println(list.get(0));//////}}


0 0