2020-09-01——Java总结

第1章 集合的概述和Collection集合

  1. 集合的概述

    集合是一个容器,用来存储和获取数据的。

    1. 为什么会出现集合类

    2. 集合类体系结构图
      集合类体系结构图

  2. Collection集合

    1. 创建Collection集合对象并添加元素

      Collection:是单列集合的顶层接口。

      Collection 表示一组对象,这些对象也称为 collection 的元素。

      一些 collection 允许有重复的元素,而另一些则不允许。

      一些 collection 是有序的,而另一些则是无序的。

      JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。

      创建Collection集合的对象,我们采用的是多态的方式,使用的是具体的ArrayList类。

      因为这个类是最常用的集合类。

      ArrayList()

      Collection:

      :是一种特殊的数据类型,泛型。这里我们会使用就可以了。

      如何使用呢?

      在出现E的地方用引用数据类型替换即可。

      举例:Collection,Collection

      1. 案例代码一:
package com.itheima_01;
import java.util.ArrayList;
import java.util.Collection;
/*
* Collection:是单列集合的顶层接口。
* Collection 表示一组对象,这些对象也称为 collection 的元素。
* 一些 collection 允许有重复的元素,而另一些则不允许。
* 一些 collection 是有序的,而另一些则是无序的。
* JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和
List)实现。

* 创建Collection集合的对象,我们采用的是多态的方式,使用的是具体的ArrayList类。
* 因为这个类是最常用的集合类。
* ArrayList() 

* Collection<E>:
*      <E>:是一种特殊的数据类型,泛型。这里我们会使用就可以了。
*      如何使用呢?
*          在出现E的地方用引用数据类型替换即可。
*          举例:Collection<String>,Collection<Student>
*/

public class CollectionDemo {
    public static void main(String[] args) {
        //创建Collection集合对象
        //JDK7的新特性,看懂就可以
        //Collection<String> c = new ArrayList<>(); //多态的方式
        Collection<String> c = new ArrayList<String>(); //多态的方式

	//boolean add(E e):添加元素
        c.add("hello");
        c.add("world");
        c.add("java");

        //输出集合对象
        System.out.println(c);
        //输出了集合中的元素按照指定格式拼接的内容,说明ArrayList重写了toString()方法
    }
}
2. Collection集合的成员方法

	boolean add(E e):添加元素

	boolean remove(Object o):从集合中移除元素

	void clear():清空集合中的元素

	boolean contains(Object o):判断集合中是否存在指定的元素

	boolean isEmpty():判断集合是否为空

	int size():集合的长度,也就是集合中元素的个数

	1. 案例代码二:
package com.itheima_01;

import java.util.ArrayList;
import java.util.Collection;

/*
 * boolean add(E e):添加元素
 * boolean remove(Object o):从集合中移除元素
 * void clear():清空集合中的元素
 * boolean contains(Object o):判断集合中是否存在指定的元素
 * boolean isEmpty():判断集合是否为空
 * int size():集合的长度,也就是集合中元素的个数
 */
public class CollectionDemo2 {
    public static void main(String[] args) {
        //创建集合对象
        Collection<String> c = new ArrayList<String>();
        
        //boolean add(E e):添加元素
        //System.out.println("add:"+c.add("hello"));
        //System.out.println("add:"+c.add("world"));
        //通过查看源码,我们知道ArrayList集合的add方法的返回值永远都是true
        c.add("hello");
        c.add("world");
        c.add("java");
        
        //boolean remove(Object o):从集合中移除元素
        //System.out.println("remove:"+c.remove("world"));
        //System.out.println("remove:"+c.remove("haha"));
        
        //void clear():清空集合中的元素
        //c.clear();
        
        //boolean contains(Object o):判断集合中是否存在指定的元素
        //System.out.println("contains:"+c.contains("world"));
        //System.out.println("contains:"+c.contains("haha"));
        
        //boolean isEmpty():判断集合是否为空
        //System.out.println("isEmpty:"+c.isEmpty());
        
        //int size():集合的长度,也就是集合中元素的个数
        System.out.println("size:"+c.size());
        
        //输出集合对象
        System.out.println(c);
    } 
}
3. Collection集合的遍历

	Collection集合的遍历

	Iterator<E> iterator():返回在此 collection 的元素上进行迭代的迭代器。

	通过集合对象调用iterator()方法得到迭代器对象。

	Iterator:

	E next():返回迭代的下一个元素。 

	boolean hasNext():如果仍有元素可以迭代,则返回 true。

	1. 案例代码三:
package com.itheima_01;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
 
/*
 * Collection集合的遍历
 * 
 * Iterator<E> iterator():返回在此 collection 的元素上进行迭代的迭代器。
 * 通过集合对象调用iterator()方法得到迭代器对象。
 * 
 * Iterator:
 *      E next():返回迭代的下一个元素。 
 *      boolean hasNext():如果仍有元素可以迭代,则返回 true。
 */
public class CollectionDemo3 {
    public static void main(String[] args) {
        //创建集合对象
        Collection<String> c = new ArrayList<String>();
        
        //添加元素
        c.add("hello");
        c.add("world");
        c.add("java");
        
        //Iterator<E> iterator()
        Iterator<String> it = c.iterator();//返回的是迭代器接口的实现类的对象
        //System.out.println(it.next());
        //System.out.println(it.next());
        //System.out.println(it.next());
        //NoSuchElementException:没有这样的元素异常
        //System.out.println(it.next());
        
        //boolean hasNext()
        while(it.hasNext()){
           //System.out.println(it.next());
           String s = it.next();
           System.out.println(s);
        }
    }
}
4. 集合使用步骤图解

集合使用步骤图解

5. Collection集合的练习存储自定义对象并遍历

	1. 案例代码四:
package com.itheima_02;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/*
 * Collection集合存储自定义对象并遍历
 * 提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。
 * 
 * 集合的使用步骤:
 *      A:创建集合对象
 *      B:创建元素对象
 *      C:把元素添加到集合
 *      D:遍历集合
 */
public class CollectionTest {
    public static void main(String[] args) {
	//创建集合对象
        Collection<Student> c = new ArrayList<Student>();
        
        //创建元素对象
        Student s1 = new Student("林青霞",30);
        Student s2 = new Student("张曼玉",35);
        Student s3 = new Student("王祖贤",33);
        
        //把元素添加到集合
        c.add(s1);
        c.add(s2);
        c.add(s3);
        
        //遍历集合
        Iterator<Student> it = c.iterator();
        while(it.hasNext()){
           Student s = it.next();
           System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

第2章 List集合

  1. List集合的特点

    List:有序的 collection(也称为序列)。

    此接口的用户可以对列表中每个元素的插入位置进行精确地控制。

    用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。

    与 set 不同,列表通常允许重复的元素。

    List集合的特点:

    A:有序(存储和取出元素的顺序一致)

    B:存储的元素可以重复

    1. 案例代码五:
package com.itheima_01;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
 * List:有序的 collection(也称为序列)。
 * 此接口的用户可以对列表中每个元素的插入位置进行精确地控制。
 * 用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
 * 与 set 不同,列表通常允许重复的元素。
 * 

 * List集合的特点:
 *      A:有序(存储和取出元素的顺序一致)
 *      B:存储的元素可以重复
 */

public class ListDemo {
    public static void main(String[] args) {
        //创建集合对象
        List<String> list = new ArrayList<String>();
        
        //存储元素
        list.add("hello");
        list.add("world");
        list.add("java");
        list.add("world");
        
        //遍历集合
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
           String s = it.next();
           System.out.println(s);
        }
    }
}
  1. List集合的特有成员方法

    void add(int index,E element):在指定位置添加元素

    E remove(int index):删除指定位置的元素

    E get(int index):获取指定位置的元素

    E set(int index,E element):修改指定位置的元素

    1. 案例代码六:
package com.itheima_01;
 
import java.util.ArrayList;
import java.util.List;
 
/*
 * void add(int index,E element):在指定位置添加元素
 * E remove(int index):删除指定位置的元素
 * E get(int index):获取指定位置的元素
 * E set(int index,E element):修改指定位置的元素
 */
public class ListDemo2 {
    public static void main(String[] args) {
        //创建集合对象
        List<String> list = new ArrayList<String>();
        
        //使用继承Collection的添加功能
        list.add("hello");
        list.add("world");
        list.add("java");
        
        //void add(int index,E element):在指定位置添加元素
        //list.add(1, "javaee");
        //IndexOutOfBoundsException
        //list.add(11,"javase");
        
        //E remove(int index):删除指定位置的元素,返回被删除的元素
        //System.out.println("remove:"+list.remove(1));
        //System.out.println("remove:"+list.remove(11));
        
        //E get(int index):获取指定位置的元素
        //System.out.println("get:"+list.get(1));
        //System.out.println("get:"+list.get(11));
        
        //E set(int index,E element):修改指定位置的元素,返回被修改的元素
        //System.out.println("set:"+list.set(1,"javaee"));
        
        //输出集合对象
        System.out.println(list);
    }
}
  1. List集合的普通for循环遍历

    List集合的遍历:

    A:迭代器

    B:普通for循环

    1. 案例代码七:
package com.itheima_01;
 
import java.util.ArrayList;
import java.util.List;
 
/*
 * List集合的遍历:
 *      A:迭代器
 *      B:普通for循环
 */
public class ListDemo3 {
    public static void main(String[] args) {
        // 创建集合对象
        List<String> list = new ArrayList<String>();
 
        // 添加元素
        list.add("hello");
        list.add("world");
        list.add("java");
 
        // E get(int index):获取指定位置的元素
        // System.out.println(list.get(0));
        // System.out.println(list.get(1));
        // System.out.println(list.get(2));
        // IndexOutOfBoundsException
        // System.out.println(list.get(3));
        //System.out.println("-----------");
        // ctrl+/ 可以对选中的代码进行单行注释,再来一次就是取消单行注释
 
        // 循环改进
        // for (int x = 0; x < 3; x++) {
        // System.out.println(list.get(x));
        // }
        
        //int size():集合的长度,也就是集合中元素的个数
        for(int x=0; x<list.size(); x++) {
           //System.out.println(list.get(x));
           String s = list.get(x);
           System.out.println(s);
        }
    }
}
  1. List集合的练习存储自定义对象并遍历

    List集合存储自定义对象并遍历

    提示:自定义一个学生类,给出成员变量name和age。

    遍历集合的时候,在控制台输出学生对象的成员变量值。

    两种方式遍历

    迭代器

    普通for

    1. 案例代码八:
package com.itheima_02;

 public class Student {
    private String name;
    private int age;
 
    public Student() {
 
    }
 
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    } 
    public void setAge(int age) {
        this.age = age;
    }
}
2. 案例代码九:
package com.itheima_02;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
/*
 * List集合存储自定义对象并遍历
 * 提示:自定义一个学生类,给出成员变量name和age。
 * 遍历集合的时候,在控制台输出学生对象的成员变量值。
 * 两种方式遍历
 *      迭代器
 *      普通for
 */

public class ListTest {
    public static void main(String[] args) {
        //创建集合对象
        List<Student> list = new ArrayList<Student>();
        
        //创建元素对象
        Student s1 = new Student("林青霞",30);
        Student s2 = new Student("张曼玉",35);
        Student s3 = new Student("王祖贤",33);
        
        //把元素添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        
        //迭代器
        Iterator<Student> it = list.iterator();
        while(it.hasNext()){
           Student s = it.next();
           System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("-----------------");
        
        //普通for
        for(int x=0; x<list.size(); x++) {
           Student s = list.get(x);
           System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}
  1. 列表迭代器的特有功能

    ListIterator:

    ListIterator listIterator():返回此列表元素的列表迭代器

    public interface ListIteratorextends Iterator

    特有功能:

    E previous():返回列表中的前一个元素。

    boolean hasPrevious():如果以逆向遍历列表,列表迭代器有多个元素,则返回 true。

    1. 案例代码十:
package com.itheima_01;
 
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
 
/*
 * ListIterator:
 *      ListIterator<E> listIterator():返回此列表元素的列表迭代器
 *      public interface ListIterator<E>extends Iterator<E>
 * 	
 * 特有功能:
 *      E previous():返回列表中的前一个元素。
 *      boolean hasPrevious():如果以逆向遍历列表,列表迭代器有多个元素,则返回 true。
 *      注意:ListIterator可以实现逆向遍历,但是要求先正向遍历,才能逆向遍历。
 */
public class ListIteratorDemo {
    public static void main(String[] args) {
        //创建集合对象
        List<String> list = new ArrayList<String>();
        
        //添加元素
        list.add("hello");
        list.add("world");
        list.add("java");
        
        ListIterator<String> lit = list.listIterator();
//      while(lit.hasNext()){
//         String s = lit.next();
//         System.out.println(s);
//      }
        System.out.println("--------------------------");
        
        while(lit.hasPrevious()) {
           String s = lit.previous();
           System.out.println(s);
        }
    }
}
  1. 并发修改异常产生的原因及解决方案

    我有一个集合:List list = new ArrayList();

    里面有三个元素list.add(“hello”);list.add(“world”);list.add(“java”);

    我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现。

    ConcurrentModificationException:当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。

    产生的原因:

    迭代器依赖于集合而存在,在判断成功后,集合中添加了新的元素,而迭代器并不知道,所有就报错了。

    其实这个问题说的是:迭代器遍历集合中的元素的时候,不要使用集合对象去修改集合中的元素。

    如何解决呢?

    A:迭代器遍历的时候,我可以通过迭代器修改集合中的元素

    元素是跟在刚才迭代的元素后面的

    B:集合遍历的时候,我可以通过集合对象修改集合中的元素

    元素是在最后添加的

    1. 案例代码十一:
package com.itheima_02;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorDemo {
    public static void main(String[] args) {
        // 创建集合对象
        List<String> list = new ArrayList<String>();
 
        // 添加元素
        list.add("hello");
        list.add("world");
        list.add("java");
        
        //我想判断里面有没有"world"这个元素,如果有,我就添加一个"javaee"元素
//      Iterator<String> it = list.iterator();
//      while(it.hasNext()){
//         String s = it.next();
//         if(s.equals("world")) {
//             list.add("javaee");
//         }
//      }
        
        //迭代器遍历的时候,我可以通过迭代器修改集合中的元素
        ListIterator<String> lit = list.listIterator();
        while(lit.hasNext()) {
           String s = lit.next();
           if(s.equals("world")) {
               lit.add("javaee");
           }
        }
        
        //集合遍历的时候,我可以通过集合对象修改集合中的元素
//      for(int x=0; x<list.size(); x++) {
//         String s = list.get(x);
//         if(s.equals("world")) {
//             list.add("javaee");
//         }
//      }
        
        System.out.println(list);
    }
}
  1. 增强for的概述和使用

    增强for:是for循环的一种

    格式:

    for(元素的数据类型 变量名 : 数组名或者Collection集合对象名) {

    使用变量名即可,这个变量名代表的其实就是数组或者Collection集合中的元素

    }

    好处:简化了数组和Collection集合的遍历

    弊端:目标不能为null。如何保证呢?在遍历前先对目标进行不为null的判断。

    1. 案例代码十二:
package com.itheima_01;

import java.util.ArrayList;
import java.util.List; 
/*
 * 增强for:是for循环的一种
 * 
 * 格式:
 *      for(元素的数据类型 变量名 : 数组名或者Collection集合对象名) {
 *          使用变量名即可,这个变量名代表的其实就是数组或者Collection集合中的元素
 *      }
 * 
 *      好处:简化了数组和Collection集合的遍历
 *      弊端:目标不能为null。如何保证呢?在遍历前先对目标进行不为null的判断。
 */

public class ForDemo {
    public static void main(String[] args) {
        //定义一个int类型的数组
        int[] arr = {1,2,3,4,5};
        //普通for
        for(int x=0; x<arr.length; x++) {
           System.out.println(arr[x]);
        }
        System.out.println("---------");
        //增强for
        for(int x : arr) {
           System.out.println(x);
        }
        System.out.println("---------");
        //定义一个String类型的数组
        String[] strArray = {"hello","world","java"};
        //增强for
        for(String s : strArray) {
           System.out.println(s);
        }
        System.out.println("---------");
        //创建集合对象
        List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("world");
        list.add("java");
        //增强for
        for(String s :list) {
           System.out.println(s);
        }
        
//      list = null;
//      //NullPointerException
//      if(list != null) {
//         for(String s :list) {
//             System.out.println(s);
//         }
//      }
       
        //增强for其实就是用来替代迭代器的
//      for(String s :list) {
//         if(s.equals("world")) {
//             list.add("javaee");
//         }
//      }        
    }
}
  1. 增强for的练习List集合存储自定义对象并遍历

    List集合存储自定义对象并遍历

    提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。

    遍历方式

    增强for

    1. 案例代码十三:
package com.itheima_02;

public class Student {
    private String name;
    private int age;
 
    public Student() {
 
    }
 
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
}
2. 案例代码十四:
package com.itheima_02;
 
import java.util.ArrayList;
import java.util.List;
 
/*
 * List集合存储自定义对象并遍历
 * 提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。
 * 遍历方式
 *      增强for
 */
public class ForTest {
    public static void main(String[] args) {
        //创建集合对象
        List<Student> list = new
ArrayList<Student>();
        
        //创建元素对象
        Student s1 = new Student("林青霞",30);
        Student s2 = new Student("张曼玉",35);
        Student s3 = new Student("王祖贤",33);
        
        //把元素添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        
        //遍历集合
        //增强for
        for(Student s : list) {
           System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

第3章 数据结构和ArrayList集合的相关案例

  1. 数据结构

    1. 常见数据结构之栈和队列
      栈和队列

    2. 常见数据结构之数据和链表
      数组和链表

  2. ArrayList集合的相关案例

    1. List集合子类特点及ArrayList集合存储字符串并遍历

      List:
      ArrayList:底层数据结构是数组,查询快,增删慢

      LinkedList:底层数据结构是链表,查询慢,增删快

      ArrayList存储字符串并遍历:

      A:迭代器

      B:普通for

      C:增强for

      1. 案例代码十五:
package com.itheima_01;

import java.util.ArrayList;
import java.util.Iterator; 
/*
 * List:
 *      ArrayList:底层数据结构是数组,查询快,增删慢
 *      LinkedList:底层数据结构是链表,查询慢,增删快
 * 
 * ArrayList存储字符串并遍历:
 *      A:迭代器
 *      B:普通for
 *      C:增强for
 */

public class ArrayListDemo {
    public static void main(String[] args) {
        //创建集合对象
        ArrayList<String> array = new ArrayList<String>();
        
        //添加元素
        array.add("hello");
        array.add("world");
        array.add("java");
        
        //迭代器
        Iterator<String> it = array.iterator();
        while(it.hasNext()){
           String s = it.next();
           System.out.println(s);
        }
        System.out.println("-------------");
        
        //普通for
        for(int x=0; x<array.size(); x++) {
           String s = array.get(x);
           System.out.println(s);
        }
        System.out.println("-------------");
        
        //增强for
        for(String s : array) {
           System.out.println(s);
        }     
    }
}
2. ArrayList集合的练习存储自定义对象并遍历

	ArrayList集合存储自定义对象并遍历
	
	提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。
	
	三种方式遍历
	
	迭代器
	
	普通for
	
	增强for
	
	
	LinkedList的使用和ArrayList的相似,所以LinkedList的练习需要大家自己做



	1. 案例代码十六:
package com.itheima_02;

public class Student {
    private String name;
    private int age;
 
    public Student() {
 
    }
 
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
}
	2. 案例代码十七:
package com.itheima_02;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/*
 * ArrayList集合存储自定义对象并遍历
 * 提示:自定义一个学生类,给出成员变量name和age。遍历集合的时候,在控制台输出学生对象的成员变量值。
 * 三种方式遍历
 *      迭代器
 *      普通for
 *      增强for
 * 
 * LinkedList的使用和ArrayList的相似,所以LinkedList的练习需要大家自己做
 */
public class ArrayListTest {
    public static void main(String[] args) {
        //创建集合对象
        ArrayList<Student> array = new ArrayList<Student>();
        
        //创建元素对象
        Student s1 = new Student("林青霞",30);
        Student s2 = new Student("张曼玉",35);
        Student s3 = new Student("王祖贤",33);
        
        //把元素添加到集合
        array.add(s1);
        array.add(s2);
        array.add(s3);
        
        //迭代器
        Iterator<Student> it = array.iterator();
        while(it.hasNext()){
           Student s = it.next();
           System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("----------------------");
        
        //普通for
        for(int x=0; x<array.size(); x++) {
           Student s = array.get(x);
           System.out.println(s.getName()+"---"+s.getAge());
        }
        System.out.println("----------------------");
        
        //增强for
        for(Student s : array) {
           System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值