做自己的网站有什么用,wordpress主页显示,360免费wifi手机版官方下载,哪家公司建站的Collection: - List(有序【指的是存储和取出的顺序是一致的】且可以发生重复#xff0c;且有索引的概念) - ArrayList#xff1a; 底层数据结构是数组#xff0c;查询快#xff0c;增删慢#xff0c;线程不安全的#xff0c;效率高。 - … Collection: - List(有序【指的是存储和取出的顺序是一致的】且可以发生重复且有索引的概念) - ArrayList 底层数据结构是数组查询快增删慢线程不安全的效率高。 - Vector底层数据结构是数组查询快增删慢线程安全的效率低 【即便Vector是线程安全的今后也不用我们会将不安全的ArrayList变成安全的】 - LinkedList底层数据结构是双链表增删快查询慢线程不安全的效率高。 - Set(元素唯一且无序) Collection成员方法 boolean add(E e) 在集合添加一个元素 boolean remove(Object o) 从集合删除一个元素如果有重复删除左边第一个 void clear() 清空集合中所有元素 boolean contains(Object o) 判断集合中是否包含某个元素 boolean isEmpty() 判断集合是否为空 int size() 获取集合中元素个数 参考使用
public class CollectionDemo1 {public static void main(String[] args) {Collection c1 new ArrayList();System.out.println(c1: c1); //重写的toString()方法来自AbstractCollection类中System.out.println(-----------------------------);//boolean add(Object e) 向集合中添加元素c1.add(100); // 涉及到自动装箱 将基本数据类型的值包装成对应的包装类类型c1.add(hello);c1.add(true);c1.add(11.23);c1.add(100);System.out.println(c1: c1);//boolean remove(Object o) 从集合中删除某个元素c1.remove(100); //若删除的元素有重复的存在只会删除最左边的第一个相同的元素System.out.println(c1: c1);//void clear() 清空集合中所有的元素c1.clear();System.out.println(c1: c1);//boolean contains(Object o) 判断集合中是否包含某个元素System.out.println(c1.contains(helldasdao));//boolean isEmpty() 判断集合中是否有元素存在System.out.println(c1.isEmpty());//int size() 获取集合中的元素个数System.out.println(c1.size());}
}List接口中特有的成员方法因为List集合有索引的概念针对索引的操作多了一些方法 void add(int index,E element) 指定位置索引添加元素到集合中 E remove(int index) 指定索引删除某个元素返回被删除的元素 E get(int index) 根据索引获取集合中的元素对象 E set(int index,E element) 指定位置索引修改元素返回原本位置上的元素对象 ListIterator listIterator() List集合专有的迭代器
参考使用
注意 //一个迭代器对象只有一个游标可以移动 //ListIterator listIterator() List集合专有的迭代器 //listIterator()通过观察子类源码发现底层是返回了一个ListItr类的对象 //ListItr类是继承自Itr类也拥有hasNext()和next()方法 //ListItr类中有hasPrevious()和previous()方法 //hasPrevious(): 判断前一个位置上是否有元素 //previous(): 将游标向前移动一位并获取位置上的元素
public class ListDemo2 {public static void main(String[] args) {//创建一个List集合对象List li1 new ArrayList();//创建元素对象Student s1 new Student(aaa, 12);Student s2 new Student(bbb, 13);Student s3 new Student(ccc, 14);Student s4 new Student(fff, 15);//将元素添加到List集合中li1.add(s1);li1.add(s2);li1.add(s3);li1.add(s4);System.out.println(li1: li1);System.out.println();//void add(int index,Object element) 指定位置索引添加元素到集合中Student s5 new Student(qqq, 18);//需求将s5添加在s2和s3之间li1.add(2,s5);Student s6 new Student(zzz, 20);li1.add(5,s6); // 紧跟着最后一个元素后面的索引可以赋值其他的索引不行System.out.println(li1: li1);
// System.out.println();//E remove(int index) 指定索引删除某个元素返回被删除的元素
// Object o li1.remove(2);
// System.out.println(li1: li1);
// System.out.println(o: o);System.out.println();//Object get(int index) 根据索引获取集合中的元素对象Object o li1.get(2);System.out.println(li1: li1);System.out.println(o);System.out.println();//Object set(int index,Object element) 指定位置索引修改元素返回原本位置上的元素对象Student s7 new Student(www, 31);Object o2 li1.set(2, s7);System.out.println(li1: li1);System.out.println(o2: o2);System.out.println();//一个迭代器对象只有一个游标可以移动//ListIterator listIterator() List集合专有的迭代器//listIterator()通过观察子类源码发现底层是返回了一个ListItr类的对象//ListItr类是继承自Itr类也拥有hasNext()和next()方法ListIterator listIterator li1.listIterator();while (listIterator.hasNext()){Object o1 listIterator.next();System.out.println(o1);}System.out.println(__________________________________________);//ListItr类中有hasPrevious()和previous()方法//hasPrevious(): 判断前一个位置上是否有元素//previous(): 将游标向前移动一位并获取位置上的元素//要想倒着遍历必选先将迭代器的游标移动到最后一位。while (listIterator.hasPrevious()){Object o3 listIterator.previous();System.out.println(o3);}}
}ArrayList包含了List的一些用法
public class ArrayListTest2 {public static void main(String[] args) {ArrayList list1 new ArrayList();Student s1 new Student(小黑, 18);Student s2 new Student(小白, 17);Student s3 new Student(小花, 18);Student s4 new Student(小红, 19);Student s5 new Student(小黑, 18);list1.add(s1);list1.add(s2);list1.add(s3);list1.add(s4);list1.add(s5);System.out.println(list1: list1);
Vector类中的特有功能 public void addElement(E obj) 从效果上来看和调用add方法一样都是在集合末尾处添加元素 public E elementAt(int index) 根据索引获取元素 从效果上来看和调用get方法一样 public Enumeration elements() 获取一个存储所有集合元素的容器类似于迭代器
Vector中Enumeration elements()参考用法如下 //public Enumeration elements() 获取一个存储所有集合元素的容器类似于迭代器// 从效果上来看和使用迭代器一样都是先判断下一个位置是否有元素然后再获取将来就使用迭代器来替代它Enumeration elements vector.elements();while (elements.hasMoreElements()){Object o elements.nextElement();System.out.println(o); LinkedList类特有功能 public void addFirst(E e)及addLast(E e) public E getFirst()及getLast() public E removeFirst()及public E removeLast()
参考用法
public class LinkedListDemo1 {public static void main(String[] args) {LinkedList list1 new LinkedList();list1.add(hello);list1.add(world);list1.add(java);list1.add(hadoop);list1.add(hello);Iterator iterator list1.iterator();while (iterator.hasNext()) {System.out.println(iterator.next());}System.out.println();//public void addFirst(E e)及addLast(E e)list1.addFirst(小美);list1.addLast(小强); // add()System.out.println(list1: list1);System.out.println();//public E getFirst()及getLast()System.out.println(list1.getFirst());System.out.println(list1.getLast());System.out.println();//public E removeFirst()及public E removeLast()System.out.println(list1.removeFirst());System.out.println(list1.removeLast());System.out.println(list1: list1);}
}