程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

java(0)

标签  

暂无标签

日期归档  

2023-06(2)

Collect集合

发布于2021-06-12 15:17     阅读(904)     评论(0)     点赞(27)     收藏(0)


集合的概念:对象的容器,实习了对对象常用的操作,类似数组的功能。

集合与数组的区别:数组长度固定,集合长度不固定;数组可以存储基本类型与引用类型,集合只能存储引用类型,存储基本类型需要采用“装箱”操作。


collection体系集合:

ArrayList:数据结构实现,查询快,增删慢;JDK1.2版本,运行效率快,线程不安全。

Vector:数据结构实现,查询快,增删慢。JDK1.0版本,运行效率慢,线程安全。

LinkedList:链表实现,增删快,查询慢。


collection常用方法:

collection集合再添加对象时,添加的是对象的地址,因此,collection调用clear或者remove方法时,只是将对象从集合中移除出去,但是对象依旧存在。


collection的demo如下:

  1. package CollectionDemo;
  2. import Student.Student;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Iterator;
  6. public class CollectionDemoFunction {
  7. public static void listFunction1() {
  8. Collection collection = new ArrayList();
  9. collection.add("苹果0");
  10. collection.add("苹果1");
  11. collection.add("苹果2");
  12. collection.add("苹果3");
  13. System.out.println("元素个数" + collection.size());
  14. System.out.println(collection);
  15. System.out.println("~~~~~~增强for~~~~~~");
  16. for (Object o: collection) {
  17. System.out.println(o);
  18. }
  19. System.out.println("~~~~~~迭代器~~~~~~");
  20. Iterator iterator = collection.iterator();
  21. while(iterator.hasNext()) {
  22. System.out.println(iterator.next());
  23. }
  24. System.out.println("元素个数" + collection.size());
  25. System.out.println("~~~~~~迭代器删除~~~~~~");
  26. Iterator iterator1 = collection.iterator();
  27. while(iterator1.hasNext()) {
  28. iterator1.next(); // 迭代器得先取出来,再删除
  29. // collection.remove("苹果0"); // 迭代器中不能中collection的方法
  30. iterator1.remove();
  31. }
  32. System.out.println("元素个数" + collection.size());
  33. }
  34. public static void listFunction2() {
  35. Collection collection = new ArrayList();
  36. Student s1 = new Student("张三1",16);
  37. Student s2 = new Student("张三2",17);
  38. Student s3 = new Student("张三3",18);
  39. Student s4 = new Student("张三4",19);
  40. collection.add(s1);
  41. collection.add(s2);
  42. collection.add(s3);
  43. collection.add(s4);
  44. System.out.println("元素个数:" + collection.size());
  45. System.out.println(collection.toString());
  46. collection.remove(s1);
  47. System.out.println("删除一个元素后的元素个数:" + collection.size());
  48. System.out.println(collection.toString());
  49. System.out.println("~~~~~~~~~~增强for~~~~~~~~~");
  50. for(Object o : collection) {
  51. Student s = (Student)o;
  52. System.out.println(s.toString());
  53. }
  54. System.out.println("~~~~~~~~~~迭代器~~~~~~~~~");
  55. Iterator iterator = collection.iterator();
  56. while(iterator.hasNext()) { // 迭代器中不能使用clooection的删除方法,迭代器中的remove方法的前面要有next方法
  57. Student s = (Student)iterator.next();
  58. System.out.println(s.toString());
  59. }
  60. System.out.println("s2是否存在?" + collection.contains(s2));
  61. }
  62. }

ArrayList是一个连续地址空间,它的demo如下:

  1. package List;
  2. import Student.Student;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.ListIterator;
  6. public class ArrayListFunction {
  7. public static void ArrayListTest1() {
  8. ArrayList arrayList = new ArrayList();
  9. Student s1 = new Student("路的话0", 160);
  10. Student s2 = new Student("路的话1", 161);
  11. Student s3 = new Student("路的话2", 162);
  12. Student s4 = new Student("路的话3", 163);
  13. arrayList.add(s1);
  14. arrayList.add(s2);
  15. arrayList.add(s3);
  16. arrayList.add(s4);
  17. System.out.println("元素个数:" + arrayList.size());
  18. System.out.println(arrayList.toString());
  19. arrayList.remove(new Student("路的话0", 160));
  20. System.out.println("元素个数:" + arrayList.size());
  21. System.out.println(arrayList.toString());
  22. System.out.println("~~~~~~~迭代器~~~~~~~~");
  23. Iterator iterator = arrayList.iterator();
  24. while(iterator.hasNext()) {
  25. Student s = (Student) iterator.next();
  26. System.out.println(s.toString());
  27. }
  28. System.out.println("~~~~~~~列表迭代器:顺序执行~~~~~~~~");
  29. ListIterator listIterator = arrayList.listIterator();
  30. while(listIterator.hasNext()) {
  31. Student s = (Student) listIterator.next();
  32. System.out.println(s.toString());
  33. }
  34. System.out.println("~~~~~~~列表迭代器:逆序执行~~~~~~~~");
  35. while(listIterator.hasPrevious()) {
  36. Student s = (Student) listIterator.previous();
  37. System.out.println(s.toString());
  38. }
  39. System.out.println("s2的位置:" + arrayList.indexOf(s2));
  40. }
  41. }

Vector是线程安全的,它的添加,移除,判空操作和上面是一样的,但是他除了可以用for,iterator来遍历外,也可以用如下方式进行遍历:

  1. public class VectorTest {
  2. public static void main(String[] args) {
  3. Vector t =new Vector();
  4. t.add("H");
  5. t.add("k");
  6. t.add("q");
  7. System.out.println("元素个数:" + t.size());
  8. System.out.println("元素:" + t.toString());
  9. // 遍历
  10. System.out.println("~~~~~~~~~~~~~~遍历~~~~~~~~~~~");
  11. Enumeration enumeration = t.elements();
  12. while(enumeration.hasMoreElements()) {
  13. String s = (String)enumeration.nextElement();
  14. System.out.println(s);
  15. }
  16. }
  17. }

linklist是一个双向链表,遍历时可以采用ListIterator进行从前往后,从后往前的遍历,demo如下:

  1. public static void main(String[] args) {
  2. LinkedList<Student> linkedList = new LinkedList<Student>();
  3. Student s1 = new Student("张三", 16);
  4. Student s2 = new Student("李四", 17);
  5. Student s3 = new Student("王五", 18);
  6. linkedList.add(s1);
  7. linkedList.add(s2);
  8. linkedList.add(s3);
  9. System.out.println("元素个数:" + linkedList.size());
  10. System.out.println("~~~~~~~~~~~for循环遍历:~~~~~~~~~~");
  11. for (int i = 0; i < linkedList.size(); i++) {
  12. Student s = (Student)linkedList.get(i);
  13. System.out.println("名字:"+ s.getName() + "; 年龄: "+ s.getAge());
  14. }
  15. System.out.println("~~~~~~~~~~~增强for循环遍历:~~~~~~~~~~");
  16. for (Object l: linkedList) {
  17. Student s = (Student)l;
  18. System.out.println(s.toString());
  19. }
  20. System.out.println("~~~~~~~~~~~迭代遍历:~~~~~~~~~~");
  21. Iterator iterator = linkedList.iterator();
  22. while(iterator.hasNext()) {
  23. Student s = (Student)iterator.next();
  24. System.out.println("名字:"+ s.getName() + "; 年龄: "+ s.getAge());
  25. }
  26. System.out.println("~~~~~~~~~~~list迭代遍历:~~~~~~~~~~");
  27. System.out.println("first-->last:");
  28. ListIterator listIterator = linkedList.listIterator();
  29. while(listIterator.hasNext()) {
  30. Student s = (Student)listIterator.next();
  31. System.out.println(s.toString());
  32. }
  33. System.out.println("last-->first:");
  34. while(listIterator.hasPrevious()) {
  35. Student s = (Student)listIterator.previous();
  36. System.out.println(s.toString());
  37. }
  38. System.out.println("是否包含张三?"+linkedList.contains(s1));
  39. System.out.println("是否为空?"+linkedList.isEmpty());
  40. }

泛型的本质:参数化类型,把类型作为参数传递,语法是<T...>,T称为类型占位符,表示一种引用类型。好处是:提高代码的重用性;防止装换异常,提高代码使用的安全性。有三种类型:泛型类,泛型接口,泛型方法。

 

 

原文链接:https://blog.csdn.net/qq_15897815/article/details/117677496



所属网站分类: 技术文章 > 博客

作者:java王侯

链接:http://www.javaheidong.com/blog/article/222541/dbd981e8ada0c222f818/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

27 0
收藏该文
已收藏

评论内容:(最多支持255个字符)