写在前边java
你们好,今天呢就继续更新 Java 中级进阶。之因此隔一天一更新是由于抽出一天的时间本身收集、整理要分享的知识点。在收集的时候,我我的很看重知识点自己值不值得去整理,而后把这些整理地知识点用“一根线”串起来。第一,方便本身记忆和使用;第二,分享给别人,让别人看起来更有条理。「文章末附带有本人原创的知识点的层次化整理连接,知识点结构更加清晰」。数组
JAVA集合框架框架
1. ArrayListide
1.1 ArrayList与数组的区别学习
数组的局限性:测试
① 若是要存放多个对象,可使用数组,可是数组有局限性。超过数组的长度就放不下,数组放不满就浪费了。this
代码:指针
1//先声明一个学生类(Student.java) 2public class Student { 3 public String name; 4 public Student () { 5 } 6// 增长一个初始化name的构造方法 7public Student (String name) { 8 this.name = name; 9} 10// 重写toString方法 11public String toString() { 12 return name; 13 } 14} 15//数组的局限性,声明测试类(Test.java) 16//声明长度是5的数组 17Student Student [] = new Student [5]; 18//不用的数组就浪费了 19Student [0] = new Student ("张三"); 20//放不下要报错 21Student [20] = new Student ("李四");
ArrayList存放对象:code
① 为了解决数组的局限性,引入容器类的概念。 最多见的容器类就是ArrayList。对象
② ArrayList 容器的容量会伴随着对象的增长,自动增加
③只须要不断往容器里增长学生对象便可,不用担忧会出现数组的边界问题。
代码:
1//容器类 ArrayList,用于存放对象 2ArrayList student = new ArrayList(); 3student .add( new Student ("张三")); 4System.out.println(student .size()); 5//只须要不断往容器里增长学生便可,不用担忧会出现数组的边界问题。 6student .add( new Student ("李四")); 7System.out.println(student .size());
1.2 经常使用方法
① add 增长「两种方法」
☛直接add对象,把对象加在最后面:student.add(new Student("姓名"));
☛在指定位置加对象:student.add(3, “李四”);
代码:
1ArrayList students= new ArrayList(); 2// 把5个对象加入到ArrayList中 3for (int i = 0; i < 5; i++) { 4studenst.add(new Student("student" + i)); 5} 6System.out.println(studenst); 7// 在指定位置增长对象 8Student stu = new Student("李四"); 9studenst.add(3, stu ); 10System.out.println(studenst.toString());
② contains 判断是否存在
☛经过方法contains 判断一个对象是否在容器中。
☛判断标准: 是不是同一个对象,而不是name是否相同。
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student" + i)); 5} 6Student stu = new Student ("李四"); 7students.add(stu ); 8System.out.println(students); 9// 判断一个对象是否在容器中 10// 判断标准: 是不是同一个对象,而不是name是否相同 11System.out.print("虽然一个新的对象名字也叫 student1,可是contains的返回是:"); 12System.out.println(students.contains(new Student("student1"))); 13System.out.print("而对stu 的判断,contains的返回是:"); 14System.out.println(students.contains(stu)); 15}
③ get 获取指定位置的对象
☛经过 get 获取指定位置的对象,若是输入的下标越界,同样会报错。
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student" + i)); 5} Studentstu= new Student("李四"); 7students.add(stu); 8//获取指定位置的对象 9System.out.println(students.get(5)); 10//若是超出了范围,依然会报错 11System.out.println(students.get(6));
④ indexOf 获取对象所处的位置
☛ indexOf 用于判断一个对象在 ArrayList 中所处的位置。
☛ 与 contains 同样,判断标准是对象是否相同,而非对象的 name 值是否相等。
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student" + i)); 5} 6Student stu = new Student("李四"); 7students.add(stu ); 8System.out.println(students); 9System.out.println("stu所处的位置:"+students.indexOf(stu)); 10System.out.println("新的学生,可是名字是\"student1\"所处的位置:"+students.indexOf(new Student("student1")));
⑤ remove 删除
☛ remove 用于把对象从ArrayList中删除:students.remove(2);
☛ remove 能够根据下标(对象)删除 ArrayList 的元素: students.remove(stu);
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student" + i)); 5} 6Student stu= new Student("李四"); 7students.add(stu); 8System.out.println(students); 9students.remove(2); 10System.out.println("删除下标是2的对象"); 11System.out.println(students); 12System.out.println("删除李四"); students.remove(stu); 14System.out.println(students);
⑥ set 替换
☛ set 用于替换指定位置的元素
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student" + i)); 5} 6Student stu= new Student ("李四"); 7students.add(stu); 8System.out.println(students); 9System.out.println("把下标是5的元素,替换为\"student5\""); 10students.set(5, new Student("student5")); 11System.out.println(students); 12}
⑦ size 获取大小
☛ size 用于获取 ArrayList 的大小
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Students("student" + i)); 5} 6Students stu= new Students("李四"); 7students.add(stu); 8System.out.println(students); 9System.out.println("获取ArrayList的大小:"); 10System.out.println(students.size());
⑧ toArray 转换为数组
☛ toArray 能够把一个 ArrayList 对象转换为数组。
☛ 注意:若是要转换为一个 Student 数组,那么须要传递一个Student 数组类型的对象给 toArray(),这样 toArray 方法才知道,你但愿转换为哪一种类型的数组,不然只能转换为 Object 数组
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Students("student" + i)); 5} 6Student stu= new Student ("李四"); 7students.add(stu); 8System.out.println(students); 9Student st[] = (Student[])students.toArray(new Student []{}); 10System.out.println("数组:" +st);
⑨ addAll 把另外一个容器全部对象都加进来
☛ addAll 把另外一个容器全部对象都加进来
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student" + i)); 5} 6System.out.println("ArrayList heros:\t" + students); 7//把另外一个容器里全部的元素,都加入到该容器里来 8ArrayList anotherstudents = new ArrayList( ); 9anotherstudents .add(new Student("student a")); 10anotherstudents .add(new Student("student b")); 11anotherstudents .add(new Student("student c")); 12System.out.println("anotherstudents students:\t" + anotherstudents ); 13students.addAll(anotherstudents ); 14System.out.println("把另外一个ArrayList的元素都加入到当前 ArrayList:"); 15System.out.println("ArrayList students:\t" + students);
⑩ clear 清空
☛ clear 清空一个 ArrayList
代码:
1ArrayList students= new ArrayList(); 2// 初始化5个对象 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student" + i)); 5} 6System.out.println("ArrayList students:\t" + students); 7System.out.println("使用clear清空"); 8students.clear(); 9System.out.println("ArrayList students:\t" + students);
1.3 List 接口
ArrayList 和 List:
① ArrayList 实现了接口 List。
② 常见的写法会把引用声明为接口 List 类型。
③ 注意:是 java.util.List,而不是 java.awt.List。
代码:
1import java.util.ArrayList; 2import java.util.List; 3import charactor.Student; 4public class TestCollection { 5public static void main(String[] args) { 6//接口引用指向子类对象(多态) 7List students = new ArrayList(); 8students .add( new Student("李四")); 9System.out.println(students .size()); 10} 11}
List接口的方法:
① 由于 ArrayList 实现了 List 接口,因此List接口的方法 ArrayList都实现了。
② 在 ArrayList 经常使用方法章节有详细的讲解
1.4 List接口
泛型 Generic:
① 不指定泛型的容器,能够存听任何类型的元素
② 指定了泛型的容器,只能存放指定类型的元素以及其子类
泛型的简写:
① 为了避免使编译器出现警告,须要先后都使用泛型
1List<Student> gen = new ArrayList<Student>();
② 不过JDK7提供了一个能够略微减小代码量的泛型简写方式
1List<Student> gen = new ArrayList<>();
1.5 遍历
用 for 循环遍历:
① 经过前面的学习,知道了能够用 size() 和 get() 分别获得大小,和获取指定位置的元素,结合 for 循环就能够遍历出 ArrayList 的内容
代码:
1List<Student> students= new ArrayList<Student>(); 2// 放5个Student进入容器 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student name " + i)); 5} 6// 第一种遍历 for循环 7System.out.println("--------for 循环-------"); 8for (int i = 0; i < students.size(); i++) { 9Student s = students.get(i); 10System.out.println(s);
迭代器遍历:
① 使用迭代器 Iterator 遍历集合中的元素
代码:
1List<Student> students= new ArrayList<Student>(); 2//放5个students进入容器 3for (int i = 0; i < 5; i++) { 4students.add(new students("student name " +i)); 5} 6//第二种遍历,使用迭代器 7System.out.println("--------使用while的iterator-------"); 8Iterator<Student> it= students.iterator(); 9//从最开始的位置判断"下一个"位置是否有数据 10//若是有就经过next取出来,而且把指针向下移动 11//直达"下一个"位置没有数据 12while(it.hasNext()){ 13Student s = it.next(); 14System.out.println(s); 15} 16//迭代器的for写法 17System.out.println("--------使用for的iterator-------"); 18for (Iterator<Student> iterator = students.iterator(); iterator.hasNext();) { 19Student student= (Student ) iterator.next(); 20System.out.println(student); 21}
用加强型 for 循环:
① 使用加强型 for 循环能够很是方便的遍历 ArrayLis t中的元素,这是不少开发人员的首选。
② 不过加强型 for 循环也有不足:没法用来进行 ArrayList 的初始化,没法得知当前是第几个元素了,当须要只打印单数元素的时候,就作不到了。 必须再自定下标变量。
代码:
1List<Student> students= new ArrayList<Student>(); 2// 放5个Student进入容器 3for (int i = 0; i < 5; i++) { 4students.add(new Student("student name " + i)); 5} 6// 第三种,加强型for循环 7System.out.println("--------加强型for循环-------"); 8for (Student s : students) { 9System.out.println(s); 10}