java 容器 List

时间:2023-03-09 02:27:45
java 容器 List

1.概念:Java容器类类库的用途是保存对象,容器中不能存储基本数据类型,必须是对象(引用数据类型)

2.为什么需要容器:主要是在以数组作为数据的存储结构中,其长度难以扩充,同时数组中元素类型必须相同。而容器可以弥补数组的这两个缺陷

3.容器框架
Java集合框架提供了一套性能优良、使用方便的接口和类,
它们位于 java.util 包中
存放在集合中的数据,被称为元素(element)

java 容器 List

各接口的特点
Collection 接口存储一组不唯一(可以重复),无序的对象
List 接口存储一组不唯一,有序(索引顺序)的对象
Set 接口存储一组唯一,无序的对象

4.ArrayList

List承诺可以将元素维护在特定的序列中。List接口在Collection的基础上加入了大量的方法,使得可以在List中间可以插入和移除元素

 import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner; public class TestArrayList {
public static void main(String[] args) {
//创建集合对象, 接口 new 实现类
List list=new ArrayList(); //(1)添加add (Object obj)
list.add("hello");
list.add(123);//自动装箱(基本数据类型自动转对应的包装类)
list.add(new Scanner(System.in)); //(2)集合中元素的个数 size()
System.out.println(list.size());//
System.out.println("集合是否为空:"+list.isEmpty());//false //(3)addAll(Collection c)
List list2=new ArrayList();
list2.add("hello");
list2.add(123);
list.addAll(list2);
System.out.println("list集合中元素的个数:"+list.size());//
System.out.println(list);
//[hello, 123, java.util.Scanner[...], hello, 123] //(4)删除
System.out.println("根据对象去删除:");
list.remove("hello");
System.out.println(list);//[123, java.util.Scanner[...], hello, 123]
//list.remove(123);//认为123是索引
list.remove(new Integer(123));
System.out.println(list);//[java.util.Scanner[...], hello, 123]
list.remove(0);//根据索引去删除
System.out.println(list);//[hello, 123]
list.add("world"); //list [hello,123,world] list2[hello,123]
//list.removeAll(list2);//[word]
//list.retainAll(list2);//[hello, 123]
System.out.println(list);//[hello, 123, world] //(5)判断
System.out.println("hello在集合中是否存在:"+list.contains("hello"));//true
System.out.println("java在集合中是否存在:"+list.contains("java"));//false //(6)清空集合中所有的元素对象
System.out.println(list);//[hello, 123, world]
System.out.println(list2);//[hello, 123]
System.out.println(list.containsAll(list2));//true----list是否包含list2
//list.clear();
//System.out.println(list); //(7)获取指定索引位置上的元素对象
System.out.println(list.get(1));//123 //(8)设置
list.set(1, "java");
System.out.println(list);//[hello, java, world] //(9)在指定的索引位置上添加元素对象
list.add(1, "html");
System.out.println(list);//[hello, html, java, world] //(10)查找元素在集合中的位置
System.out.println(list.indexOf("java")+"\t"+list.indexOf("sql"));//2 -1
//(11)遍历集合中元素的内容
/**(1)使用加强for循环遍历集合中的元素*/
System.out.println("\n使用加强for循环遍历集合中的元素\n");
for(Object obj:list){
System.out.println(obj); }
/**(2)使用普通for循环遍历集合中的元素对象*/
System.out.println("\n使用普通for循环遍历集合中的元素对象\n");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
/**(3)使用迭代器*/
System.out.println("\n使用迭代器遍历集合中的元素\n");
Iterator ite=list.iterator(); //正向遍历
while(ite.hasNext()){//判断集合中是否有元素对象
Object obj=ite.next();
System.out.println(obj);
}
System.out.println("使用listIterator()遍历");
ListIterator listIte=list.listIterator();
System.out.println("正向遍历");
System.out.println("在集合的开头,后面还有元素对象吗?"+listIte.hasNext());
System.out.println("在集合的开头,前面有元素对象吗?"+listIte.hasPrevious());
while(listIte.hasNext()){
System.out.println(listIte.next());
}
System.out.println("到达集合的末尾,后面还有元素对象吗?"+listIte.hasNext());
System.out.println("到达集合的末尾,前面有元素对象吗?"+listIte.hasPrevious());
System.out.println("\n逆向遍历集合中的元素\n");
while(listIte.hasPrevious()){
System.out.println(listIte.previous());
} }
}