发布于2021-06-12 15:17 阅读(904) 评论(0) 点赞(27) 收藏(0)
集合的概念:对象的容器,实习了对对象常用的操作,类似数组的功能。
集合与数组的区别:数组长度固定,集合长度不固定;数组可以存储基本类型与引用类型,集合只能存储引用类型,存储基本类型需要采用“装箱”操作。
collection体系集合:
ArrayList:数据结构实现,查询快,增删慢;JDK1.2版本,运行效率快,线程不安全。
Vector:数据结构实现,查询快,增删慢。JDK1.0版本,运行效率慢,线程安全。
LinkedList:链表实现,增删快,查询慢。
collection常用方法:
collection集合再添加对象时,添加的是对象的地址,因此,collection调用clear或者remove方法时,只是将对象从集合中移除出去,但是对象依旧存在。
collection的demo如下:
- package CollectionDemo;
-
- import Student.Student;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Iterator;
-
- public class CollectionDemoFunction {
-
- public static void listFunction1() {
- Collection collection = new ArrayList();
- collection.add("苹果0");
- collection.add("苹果1");
- collection.add("苹果2");
- collection.add("苹果3");
-
- System.out.println("元素个数" + collection.size());
- System.out.println(collection);
-
- System.out.println("~~~~~~增强for~~~~~~");
- for (Object o: collection) {
- System.out.println(o);
- }
-
- System.out.println("~~~~~~迭代器~~~~~~");
- Iterator iterator = collection.iterator();
- while(iterator.hasNext()) {
- System.out.println(iterator.next());
- }
-
- System.out.println("元素个数" + collection.size());
-
- System.out.println("~~~~~~迭代器删除~~~~~~");
- Iterator iterator1 = collection.iterator();
- while(iterator1.hasNext()) {
- iterator1.next(); // 迭代器得先取出来,再删除
- // collection.remove("苹果0"); // 迭代器中不能中collection的方法
- iterator1.remove();
- }
- System.out.println("元素个数" + collection.size());
- }
-
- public static void listFunction2() {
- Collection collection = new ArrayList();
- Student s1 = new Student("张三1",16);
- Student s2 = new Student("张三2",17);
- Student s3 = new Student("张三3",18);
- Student s4 = new Student("张三4",19);
-
- collection.add(s1);
- collection.add(s2);
- collection.add(s3);
- collection.add(s4);
-
- System.out.println("元素个数:" + collection.size());
- System.out.println(collection.toString());
-
- collection.remove(s1);
- System.out.println("删除一个元素后的元素个数:" + collection.size());
- System.out.println(collection.toString());
-
- System.out.println("~~~~~~~~~~增强for~~~~~~~~~");
- for(Object o : collection) {
- Student s = (Student)o;
- System.out.println(s.toString());
- }
-
- System.out.println("~~~~~~~~~~迭代器~~~~~~~~~");
- Iterator iterator = collection.iterator();
- while(iterator.hasNext()) { // 迭代器中不能使用clooection的删除方法,迭代器中的remove方法的前面要有next方法
- Student s = (Student)iterator.next();
- System.out.println(s.toString());
- }
-
- System.out.println("s2是否存在?" + collection.contains(s2));
- }
- }
ArrayList是一个连续地址空间,它的demo如下:
- package List;
-
- import Student.Student;
-
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.ListIterator;
-
- public class ArrayListFunction {
- public static void ArrayListTest1() {
- ArrayList arrayList = new ArrayList();
- Student s1 = new Student("路的话0", 160);
- Student s2 = new Student("路的话1", 161);
- Student s3 = new Student("路的话2", 162);
- Student s4 = new Student("路的话3", 163);
- arrayList.add(s1);
- arrayList.add(s2);
- arrayList.add(s3);
- arrayList.add(s4);
-
- System.out.println("元素个数:" + arrayList.size());
- System.out.println(arrayList.toString());
-
- arrayList.remove(new Student("路的话0", 160));
-
- System.out.println("元素个数:" + arrayList.size());
- System.out.println(arrayList.toString());
-
- System.out.println("~~~~~~~迭代器~~~~~~~~");
- Iterator iterator = arrayList.iterator();
- while(iterator.hasNext()) {
- Student s = (Student) iterator.next();
- System.out.println(s.toString());
- }
-
- System.out.println("~~~~~~~列表迭代器:顺序执行~~~~~~~~");
- ListIterator listIterator = arrayList.listIterator();
- while(listIterator.hasNext()) {
- Student s = (Student) listIterator.next();
- System.out.println(s.toString());
- }
-
- System.out.println("~~~~~~~列表迭代器:逆序执行~~~~~~~~");
- while(listIterator.hasPrevious()) {
- Student s = (Student) listIterator.previous();
- System.out.println(s.toString());
- }
-
- System.out.println("s2的位置:" + arrayList.indexOf(s2));
- }
- }
Vector是线程安全的,它的添加,移除,判空操作和上面是一样的,但是他除了可以用for,iterator来遍历外,也可以用如下方式进行遍历:
- public class VectorTest {
- public static void main(String[] args) {
- Vector t =new Vector();
- t.add("H");
- t.add("k");
- t.add("q");
- System.out.println("元素个数:" + t.size());
- System.out.println("元素:" + t.toString());
-
- // 遍历
- System.out.println("~~~~~~~~~~~~~~遍历~~~~~~~~~~~");
- Enumeration enumeration = t.elements();
- while(enumeration.hasMoreElements()) {
- String s = (String)enumeration.nextElement();
- System.out.println(s);
- }
- }
- }
linklist是一个双向链表,遍历时可以采用ListIterator进行从前往后,从后往前的遍历,demo如下:
- public static void main(String[] args) {
- LinkedList<Student> linkedList = new LinkedList<Student>();
- Student s1 = new Student("张三", 16);
- Student s2 = new Student("李四", 17);
- Student s3 = new Student("王五", 18);
-
- linkedList.add(s1);
- linkedList.add(s2);
- linkedList.add(s3);
-
- System.out.println("元素个数:" + linkedList.size());
-
-
- System.out.println("~~~~~~~~~~~for循环遍历:~~~~~~~~~~");
- for (int i = 0; i < linkedList.size(); i++) {
- Student s = (Student)linkedList.get(i);
- System.out.println("名字:"+ s.getName() + "; 年龄: "+ s.getAge());
- }
-
- System.out.println("~~~~~~~~~~~增强for循环遍历:~~~~~~~~~~");
- for (Object l: linkedList) {
- Student s = (Student)l;
- System.out.println(s.toString());
- }
-
- System.out.println("~~~~~~~~~~~迭代遍历:~~~~~~~~~~");
- Iterator iterator = linkedList.iterator();
- while(iterator.hasNext()) {
- Student s = (Student)iterator.next();
- System.out.println("名字:"+ s.getName() + "; 年龄: "+ s.getAge());
- }
-
- System.out.println("~~~~~~~~~~~list迭代遍历:~~~~~~~~~~");
- System.out.println("first-->last:");
- ListIterator listIterator = linkedList.listIterator();
- while(listIterator.hasNext()) {
- Student s = (Student)listIterator.next();
- System.out.println(s.toString());
- }
-
- System.out.println("last-->first:");
- while(listIterator.hasPrevious()) {
- Student s = (Student)listIterator.previous();
- System.out.println(s.toString());
- }
-
- System.out.println("是否包含张三?"+linkedList.contains(s1));
-
- System.out.println("是否为空?"+linkedList.isEmpty());
-
- }
泛型的本质:参数化类型,把类型作为参数传递,语法是<T...>,T称为类型占位符,表示一种引用类型。好处是:提高代码的重用性;防止装换异常,提高代码使用的安全性。有三种类型:泛型类,泛型接口,泛型方法。
原文链接:https://blog.csdn.net/qq_15897815/article/details/117677496
作者:java王侯
链接:http://www.javaheidong.com/blog/article/222541/dbd981e8ada0c222f818/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!