List 的使用
来源:互联网 发布:html5游戏前景知乎 编辑:程序博客网 时间:2024/05/22 14:50
List Api文档在线地址: List
举例试用:
import java.util.ArrayList;import java.util.List;import java.util.ListIterator;public class Main{ public static void main(String args[]) throws Exception { List<Integer> s=new ArrayList<Integer>(); //1. boolean add(E e) // 向列表的尾部添加指定的元素(可选操作)。 s.add(1); s.add(2); s.add(3); System.out.println("1. add(E e)"); System.out.println("操作顺序插入 add(1),add(2),add(3):"); System.out.println(s); System.out.println(); //2. void add(int index, E element) // 在列表的指定位置插入指定元素(可选操作)。 s.add(1,99); System.out.println("2. add(int index,E e)"); System.out.println("指定位置插入 add(1,99):"); System.out.println(s); System.out.println(); //3. boolean addAll(Collection<? extends E> c) //添加指定 collection 中的所有元素到此列表的结尾 List<Integer> f1=new ArrayList<Integer>(); f1.add(1);f1.add(2);f1.add(3); s.addAll(f1); System.out.println("3. addAll(Collection<? extends E> c)"); System.out.println("在指定容器后插入 [1,2,3]:"); System.out.println(s); System.out.println(); //4. boolean addAll(int index, Collection<? extends E> c) //将指定 collection 中的所有元素都插入到列表中的指定位置(可选操作)。 List<Integer> f2=new ArrayList<Integer>(); f2.add(111);f2.add(222);f2.add(333); s.addAll(1,f2); System.out.println("4. addAll(Collection<? extends E> c)"); System.out.println("在指定容器指定位置后插入 1,[111,222,333]:"); System.out.println(s); System.out.println(); //5. voidclear() //从列表中移除所有元素(可选操作)。 s.clear(); System.out.println("5. void clear()"); System.out.println("移除所有元素:"); System.out.println(s); System.out.println(); //6. boolean contains(Object o) //如果列表包含指定的元素,则返回 true。 s.add(111);s.add(222);s.add(333); System.out.println("6. boolean contains(Object o)"); System.out.println("如果列表包含指定的元素,则返回 true:"); System.out.println(s); System.out.println("contains(222):"+s.contains(222)); System.out.println("contains(444):"+s.contains(444)); System.out.println(); //7. boolean containsAll(Collection<?> c) //如果列表包含指定 collection 的所有元素,则返回 true。 System.out.println("7. boolean containsAll(Collection<?> c)"); System.out.println("如果列表包含指定 collection 的所有元素,则返回 true "); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(2); System.out.println(" (1) s:[1,2,3] f:[1,2]:"); System.out.println(" s.containsAll(f):"+s.containsAll(f1)); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(2);f1.add(4); System.out.println(" (2) s:[1,2,3] f:[1,2,4]:"); System.out.println(" s.containsAll(f):"+s.containsAll(f1)); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(4);f1.add(5);f1.add(6); System.out.println(" (3) s:[1,2,3] f:[4,5,6]:"); System.out.println(" s.containsAll(f):"+s.containsAll(f1)); System.out.println(); //8. boolean equals(Object o) //比较指定的对象与列表是否相等。 System.out.println("8. boolean equals(Object o)"); System.out.println("比较指定的对象与列表是否相等"); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(3);f1.add(2); System.out.println(" (1) s:[1,2,3] f:[1,3,2]:"); System.out.println(" s.equals(f):"+s.equals(f1)); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(2);f1.add(3); System.out.println(" (2) s:[1,2,3] f:[1,2,3]:"); System.out.println(" s.equals(f):"+s.equals(f1)); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3);s.add(4); f1.add(1);f1.add(2);f1.add(3); System.out.println(" (3) s:[1,2,3,4] f:[1,2,3]:"); System.out.println(" s.equals(f):"+s.equals(f1)); System.out.println(); //9. E get(int index) //返回列表中指定位置的元素。 s.clear(); s.add(111);s.add(222);s.add(333); System.out.println("9. E get(int index)"); System.out.println("返回列表中指定位置的元素"); System.out.println(s); System.out.println("获取 get(0):"+s.get(0)); System.out.println("获取 get(1):"+s.get(1)); System.out.println(); //10. int hashCode() //返回列表的哈希码值。 s.clear(); s.add(111);s.add(222);s.add(333); System.out.println("10. int hashCode()"); System.out.println("返回列表的哈希码值"); System.out.println(s); System.out.println("hashCode():"+s.hashCode()); System.out.println(); //11. int indexOf(Object o) //返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。 s.clear(); s.add(111);s.add(222);s.add(333);s.add(111); System.out.println("11. int indexOf(Object o)"); System.out.println("返回此列表中第一次出现的指定元素的索引"); System.out.println("如果此列表不包含该元素,则返回 -1"); System.out.println(s); System.out.println("indexOf(111):"+s.indexOf(111)); System.out.println("indexOf(444):"+s.indexOf(444)); System.out.println(); //12. boolean isEmpty() //如果列表不包含元素,则返回 true。 System.out.println("12. boolean isEmpty() "); System.out.println("如果列表不包含元素,则返回 true"); s.clear(); System.out.println(s+" s.isEmpty():"+s.isEmpty()); s.add(1);s.add(2);s.add(3); System.out.println(s+" s.isEmpty():"+s.isEmpty()); System.out.println(); //13 Iterator<E> iterator() //返回按适当顺序在列表的元素上进行迭代的迭代器。 System.out.println("13. Iterator<E> iterator()"); System.out.println("返回按适当顺序在列表的元素上进行迭代的迭代器"); System.out.println("迭代器的使用:http://blog.csdn.net/qq_35146878/article/details/78283488"); System.out.println(); //14. int lastIndexOf(Object o) //返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。 s.clear(); s.add(111);s.add(222);s.add(333);s.add(111); System.out.println("14. int lastIndexOf(Object o)"); System.out.println("返回此列表中最后出现的指定元素的索引"); System.out.println("如果此列表不包含该元素,则返回 -1"); System.out.println(s); System.out.println("lastIndexOf(111):"+s.lastIndexOf(111)); System.out.println("lastIndexOf(444):"+s.lastIndexOf(444)); System.out.println(); //15. ListIterator<E>listIterator() // 返回此列表元素的列表迭代器(按适当顺序) System.out.println("15. ListIterator<E> listIterator()"); System.out.println("返回此列表元素的列表迭代器(按适当顺序) "); s.clear(); s.add(111);s.add(222);s.add(333); System.out.println(s); System.out.println("ListIterator<Integer> f=s.listIterator();"); ListIterator<Integer> f3=s.listIterator(); System.out.print("f 一个个向后 next: "); while(f3.hasNext()) { System.out.print(f3.next()+" "); } System.out.println(); System.out.print("f 已经到底了 一个个向前 previous: "); while(f3.hasPrevious()) { System.out.print(f3.previous()+" "); } System.out.println(); System.out.println(); //16. ListIterator<E>listIterator(int index) //返回列表中元素的列表迭代器(按适当顺序),从列表的指定位置开始。 System.out.println("16. ListIterator<E> listIterator()"); System.out.println("返回此列表元素的列表迭代器(按适当顺序),从列表的指定位置开始"); System.out.println("功能与楼上相似,但是是从指定位置开始"); System.out.println(); //17. E remove(int index) //移除列表中指定位置的元素(可选操作)。 System.out.println("17. E remove(int index)"); System.out.println("移除列表中指定位置的元素(可选操作)"); s.clear(); s.add(111);s.add(222);s.add(333); System.out.println(s); System.out.println("s.remove(1):"+s.remove(1)+" 移除后:"+s); System.out.println(); //18. boolean remove(Object o) //从此列表中移除第一次出现的指定元素(如果存在)(可选操作) System.out.println("18. boolean remove(Object o) "); System.out.println("从此列表中移除第一次出现的指定元素"); s.clear(); s.add(111);s.add(222);s.add(333);s.add(111); System.out.println(s); System.out.println("s.remove(111):"+s.remove(new Integer(111))+" 移除后:"+s); System.out.println(); //19. boolean removeAll(Collection<?> c) //从列表中移除指定 collection 中包含的其所有元素(可选操作)。 System.out.println("19. boolean removeAll(Collection<?> c)"); System.out.println("从列表中移除指定 collection 中包含的其所有元素"); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(3);f1.add(2); System.out.println(" (1) s:[1,2,3] f:[1,3,2]:"); System.out.println(" s.removeAll(f):"+s.removeAll(f1)+" 移除后:"+s); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(2);f1.add(5); System.out.println(" (2) s:[1,2,3] f:[1,2,5]:"); System.out.println(" s.removeAll(f):"+s.removeAll(f1)+" 移除后:"+s); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3);s.add(4); f1.add(1);f1.add(2);f1.add(3); System.out.println(" (3) s:[1,2,3,4] f:[1,2,3]:"); System.out.println(" s.removeAll(f):"+s.removeAll(f1)+" 移除后:"+s); System.out.println(); //20. boolean retainAll(Collection<?> c) //仅在列表中保留指定 collection 中所包含的元素(可选操作)。 System.out.println("20. boolean retainAll(Collection<?> c)"); System.out.println("仅在列表中保留指定 collection 中所包含的元素"); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(3);f1.add(2); System.out.println(" (1) s:[1,2,3] f:[1,3,2]:"); System.out.println(" s.retainAll(f):"+s.retainAll(f1)+" 移除后:"+s); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3); f1.add(1);f1.add(2);f1.add(5); System.out.println(" (2) s:[1,2,3] f:[1,2,5]:"); System.out.println(" s.retainAll(f):"+s.retainAll(f1)+" 移除后:"+s); s.clear();f1.clear(); s.add(1);s.add(2);s.add(3);s.add(4); f1.add(1);f1.add(2);f1.add(3); System.out.println(" (3) s:[1,2,3,4] f:[1,2,3]:"); System.out.println(" s.retainAll(f):"+s.retainAll(f1)+" 移除后:"+s); System.out.println(); //21. E set(int index, E element) //用指定元素替换列表中指定位置的元素(可选操作)。 System.out.println("21. E set(int index, E element)"); System.out.println("用指定元素替换列表中指定位置的元素"); s.clear(); s.add(111);s.add(222);s.add(333); System.out.println(s); System.out.println("s.set(1,999):"+s.set(1,999)+" 操作后:"+s); System.out.println(); //22. intsize() //返回列表中的元素数。 System.out.println("22. int size() "); System.out.println("返回列表中的元素数"); s.clear(); s.add(111);s.add(222);s.add(333); System.out.println(s); System.out.println("s.size():"+s.size()); System.out.println(); //23. List<E> subList(int fromIndex, int toIndex) //返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图。 System.out.println("23. List<E> subList(int fromIndex, int toIndex) "); System.out.println("返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图"); s.clear(); s.add(000);s.add(111);s.add(222);s.add(333);s.add(444);s.add(555); System.out.println("s:"+s); f1.clear(); System.out.println("进行操作: f.addAll(s.subList(1,4)):"); f1.addAll(s.subList(1,4)); System.out.println("f:"+f1); System.out.println(); //24. Object[] toArray(T[] a) //返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素) System.out.println("24. Object[] toArray(T[] a) "); System.out.println("返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)"); s.clear(); s.add(000);s.add(111);s.add(222);s.add(333);s.add(444);s.add(555); System.out.println(s); Integer[] arr = s.toArray(new Integer[0]); System.out.print("arr: "); for(int i=0;i<arr.length;i++) System.out.print(arr[i].intValue()+" "); System.out.println(); }}
运行结果:
1. add(E e)操作顺序插入 add(1),add(2),add(3):[1, 2, 3]2. add(int index,E e)指定位置插入 add(1,99):[1, 99, 2, 3]3. addAll(Collection<? extends E> c)在指定容器后插入 [1,2,3]:[1, 99, 2, 3, 1, 2, 3]4. addAll(Collection<? extends E> c)在指定容器指定位置后插入 1,[111,222,333]:[1, 111, 222, 333, 99, 2, 3, 1, 2, 3]5. void clear()移除所有元素:[]6. boolean contains(Object o)如果列表包含指定的元素,则返回 true:[111, 222, 333]contains(222):truecontains(444):false7. boolean containsAll(Collection<?> c)如果列表包含指定 collection 的所有元素,则返回 true (1) s:[1,2,3] f:[1,2]: s.containsAll(f):true (2) s:[1,2,3] f:[1,2,4]: s.containsAll(f):false (3) s:[1,2,3] f:[4,5,6]: s.containsAll(f):false8. boolean equals(Object o)比较指定的对象与列表是否相等 (1) s:[1,2,3] f:[1,3,2]: s.equals(f):false (2) s:[1,2,3] f:[1,2,3]: s.equals(f):true (3) s:[1,2,3,4] f:[1,2,3]: s.equals(f):false9. E get(int index)返回列表中指定位置的元素[111, 222, 333]获取 get(0):111获取 get(1):22210. int hashCode()返回列表的哈希码值[111, 222, 333]hashCode():14367711. int indexOf(Object o)返回此列表中第一次出现的指定元素的索引如果此列表不包含该元素,则返回 -1[111, 222, 333, 111]indexOf(111):0indexOf(444):-112. boolean isEmpty() 如果列表不包含元素,则返回 true[] s.isEmpty():true[1, 2, 3] s.isEmpty():false13. Iterator<E> iterator()返回按适当顺序在列表的元素上进行迭代的迭代器迭代器的使用:http://blog.csdn.net/qq_35146878/article/details/7828348814. int lastIndexOf(Object o)返回此列表中最后出现的指定元素的索引如果此列表不包含该元素,则返回 -1[111, 222, 333, 111]lastIndexOf(111):3lastIndexOf(444):-115. ListIterator<E> listIterator()返回此列表元素的列表迭代器(按适当顺序) [111, 222, 333]ListIterator<Integer> f=s.listIterator();f 一个个向后 next: 111 222 333 f 已经到底了 一个个向前 previous: 333 222 111 16. ListIterator<E> listIterator()返回此列表元素的列表迭代器(按适当顺序),从列表的指定位置开始功能与楼上相似,但是是从指定位置开始17. E remove(int index)移除列表中指定位置的元素(可选操作)[111, 222, 333]s.remove(1):222 移除后:[111, 333]18. boolean remove(Object o) 从此列表中移除第一次出现的指定元素[111, 222, 333, 111]s.remove(111):true 移除后:[222, 333, 111]19. boolean removeAll(Collection<?> c)从列表中移除指定 collection 中包含的其所有元素 (1) s:[1,2,3] f:[1,3,2]: s.removeAll(f):true 移除后:[] (2) s:[1,2,3] f:[1,2,5]: s.removeAll(f):true 移除后:[3] (3) s:[1,2,3,4] f:[1,2,3]: s.removeAll(f):true 移除后:[4]20. boolean retainAll(Collection<?> c)仅在列表中保留指定 collection 中所包含的元素 (1) s:[1,2,3] f:[1,3,2]: s.retainAll(f):false 移除后:[1, 2, 3] (2) s:[1,2,3] f:[1,2,5]: s.retainAll(f):true 移除后:[1, 2] (3) s:[1,2,3,4] f:[1,2,3]: s.retainAll(f):true 移除后:[1, 2, 3]21. E set(int index, E element)用指定元素替换列表中指定位置的元素[111, 222, 333]s.set(1,999):222 操作后:[111, 999, 333]22. int size() 返回列表中的元素数[111, 222, 333]s.size():323. List<E> subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex(包括 )和 toIndex(不包括)之间的部分视图s:[0, 111, 222, 333, 444, 555]进行操作: f.addAll(s.subList(1,4)):f:[111, 222, 333]24. Object[] toArray(T[] a) 返回按适当顺序包含列表中的所有元素的数组(从第一个元素到最后一个元素)[0, 111, 222, 333, 444, 555]arr: 0 111 222 333 444 555
阅读全文
0 0
- List box的使用
- list的使用
- 有关List的使用
- list control的使用
- STL list的使用
- stl list的使用
- std::list的使用
- List<T>的使用
- List的简单使用
- List的简单使用
- List的使用示例
- List排序的使用
- List排序的使用
- java List的使用
- C# List的使用
- map-list的使用
- List<T>的使用
- LIst的使用
- 请帮助投票
- 移动端车牌识别前端识别算法的典型应用
- MAC 常用软件汇总
- 用*输出各种图形
- [iOS] 隐藏UITableView不需要的分割线(只显示有行内容的分割线)
- List 的使用
- 剑指offer之二叉树的镜像(Python)
- 5个文献免费下载神器
- 理解神经网络,从简单的例子开始(2)使用python建立多层神经网络
- Thymeleaf小小配置
- HashMap实现原理
- [NOIP2017模拟]放盒子
- python 利用 xlwt 生成excle
- java反射机制(一)