java基础知识(对象数组 集合 List的三个子类(ArrayList Vector LinkedList )泛型 可变参数 增强for循环))

java第十五天之学到辽~

1.1 集合
为什么会有集合?
  面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提
供了集合类
数组和集合的区别
* 长度区别:
	数组的长度是固定的而集合的长度是可变的
	
* 存储数据类型的区别:
	数组可以存储基本数据类型 , 也可以存储引用数据类型; 而集合只能存储引用数据类型
	
* 内容区别:
	数组只能存储同种数据类型的元素 ,集合可以存储不同类型的元素
1.2 Collection集合
添加功能
* boolean add(Object obj):添加一个元素

* boolean addAll(Collection c):添加一个集合的元素  (给一个集合添加进另一个集合中的所有元素)
删除功能
* void clear():移除所有元素

* boolean remove(Object o):移除一个元素

* boolean removeAll(Collection c):移除一个集合的元素(移除一个以上返回的就是true) 删除的元素是两个集合的交集元素 
  如果没有交集元素 则删除失败 返回false
判断功能
* boolean contains(Object o):判断集合中是否包含指定的元素	

* boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(这个集合 包含 另一个集合中所有的元素才算包含 才返回true)
  例:1,2,3 containsAll 12=true   1,2,3 containsAll 2,3,4=false
  
* boolean isEmpty():判断集合是否为空
获取功能
* Iterator<E> iterator()(重点)  迭代器
长度功能
* int size():元素的个数
交集功能
* A集合对B集合取交集,获取到的交集元素在A集合中。返回的布尔值表示的是A集合是否发生变化 
  boolean retainAll(Collection c):获取两个集合的交集元素(交集:两个集合都有的元素)
把集合转换为数组
* Object[] toArray()
例子:集合的遍历之集合转数组遍历

把集合转成数组toArray(),遍历这个数组 可以实现集合的遍历

public class MyTest8 {
 public static void main(String[] args) {
     Collection collection = new ArrayList();
     collection.add("hh");
     collection.add("aa");
     collection.add("bb");
     collection.add("cc");
     collection.add("dd");
     collection.add("ee");
     //把集合中的元素,放到数组中
   /*  String[] strings = new String[collection.size()];
     Iterator iterator = collection.iterator();
     int index=0;
     while (iterator.hasNext()) {
         Object ele = iterator.next();
         strings[index++]= (String) ele;
     }

     System.out.println(Arrays.toString(strings));*/
   //把集合转成数组
     Object[] objects = collection.toArray();

     System.out.println(Arrays.toString(objects));


 }
}
Collection集合存储字符串并遍历(迭代器)
public class MyTest6 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add("王王");
        collection.add("章章");
        collection.add("丽丽");
        collection.add("敏敏");
      
        //获取迭代器
     
        Iterator iterator = collection.iterator();
        System.out.println(iterator);
    
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
        }
    }
}
Collection集合存储自定义对象并遍历(迭代器)
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 int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class MyTest6 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(new Student("丽丽",20));
        collection.add("new Student("章章",15);
      
        //获取迭代器
     
        Iterator iterator = collection.iterator();
        System.out.println(iterator);
    
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
        }
    }
}
1.3 List集合
List概述及特点
* 元素有序,并且每一个元素都存在一个索引.元素可以重复.
List遍历
public class learn3 {
    public static void main(String[] args) {
//  请编写程序,存储3个学生对象到List集合中,
//        a) 使用迭代器进行遍历
//        b) 使用size()和get()方法结合进行遍历
//        c) 使用列表迭代器进行遍历
        List list = new ArrayList();
        list.add(new Student("啊哈",20));
        list.add(new Student("嗯哼",21));
        list.add(new Student("咳咳",22));
        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
        }
        System.out.println("------------------------------");
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            System.out.println(o);
        }
        System.out.println("-----------------------------");
        ListIterator listIterator = list.listIterator();
        while(listIterator.hasNext()){
            System.out.println(listIterator.next());
        }

    }
}
1.4 并发修改异常产生的原因及解决方案
ConcurrentModificationException出现
Iterator这个迭代器遍历采用hasNext方法和next方法,集合修改集合
会出现并发修改异常
原因
迭代依赖于集合 当我们往集合中添加好了元素之后 获取迭代器  那么迭代器已经知道了集合的元素个数
在遍历的时候又突然想给 集合里面加一个元素(用的是集合的add方法) 那迭代器不同意 就报错了
解决方案
* 用ListIterator迭代器遍历 用迭代器自带的add方法添加元素 那就不会报错了
  a:迭代器迭代元素,迭代器修改元素(ListIterator的特有功能add)
  b:集合遍历元素,集合修改元素

* 使用for循环遍历集合 添加元素 不会报错
1.5 List的三个子类的特点
ArrayList:
* 底层数据结构是数组,查询快,增删慢。
* 线程不安全,效率高。
Vector:
* 底层数据结构是数组,查询快,增删慢。
* 线程安全,效率低。
LinkedList:
* 底层数据结构是链表,查询慢,增删快。
* 线程不安全,效率高
1.6 泛型
泛型概述
是一种把类型明确的工作,推迟到创建对象,或者调用方法的时候才去明确的特殊的类型
参数化类型,把类型当作参数一样的传递。
泛型的格式
<数据类型>	这里的数据类型只能是引用数据类型
泛型好处
* 把运行时期的问题提前到了编译期间
* 避免了强制类型转换
* 优化了程序设计,解决了黄色警告线
注意:泛型只在编译期有效  但在运行期就擦除了
1.7 泛型类的概述及使用
* 泛型类概述:	 把泛型定义在类上
* 定义格式:		 public class 类名<泛型类型1,…>
* 注意事项:		 泛型类型必须是引用类型

例子
public class MyClass<T> { //泛型<T>  我把泛型加在类上
    T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}
public class MyTest2 {
    public static void main(String[] args) {
        把类型明确工作,推迟到创建对象,或调用方法时,才去明确的一种机制
        MyClass<String> stringMyClass = new MyClass<>();
        stringMyClass.setT("abc");
        String t = stringMyClass.getT();


        MyClass<Integer> integerMyClass = new MyClass<>();
        integerMyClass.setT(100);
        Integer t1 = integerMyClass.getT();

    }
}
1.8 集合框架(泛型方法的概述和使用)
* 泛型方法概述:	把泛型定义在方法上
* 定义格式:		public <泛型类型> 返回类型 方法名(泛型类型 变量名)

1.9 集合框架(泛型接口的概述和使用)
* 泛型接口概述:	把泛型定义在接口上
* 定义格式:		public interface 接口名<泛型类型>

例子
public interface MyInterface<T,U,R> { //泛型接口
    public R show(T t,U u);
}
public class MyDemo2<T, U, R> implements MyInterface<T, U, R>{
    @Override
    public R show(T t, U u) {
        return null;
    }
}
public class MyTest3 {
    public static void main(String[] args) {
        //接口上的泛型,在你创建该接口的子类对象时,必须要明确这个泛型,具体是什么数据类型
        MyDemo2<Integer, String, String> integerStringStringMyDemo2 = new MyDemo2<>();

        Collection<Integer> collection=new ArrayList<Integer>();


            //匿名内部类 在创建接口的子类对象时,就必须明确接口上的泛型,到底是什么类型
        new MyInterface<String,String,String>(){


            @Override
            public String show(String s, String s2) {
                return null;
            }
        };
    }
}

1.10 增强for的概述和使用
增强for概述
简化数组和Collection集合的遍历
格式
for(元素数据类型 变量 : 数组或者Collection集合) {
	使用变量即可,该变量就是元素
}
遍历
编写程序,将自定义对象存储到ArrayList集合,使用泛型并遍历

a) 使用迭代器遍历
b) 使用列表迭代器遍历
c) 使用size()和get()方法遍历
d) 使用增强for遍历

public class learn1 {
    public static void main(String[] args) {
 
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("啊哈",4));
        students.add(new Student("嗯哼",8));
        students.add(new Student("丫丫",4));
        students.add(new Student("二狗子",4));
        students.add(new Student("叮咚",4));
        //使用迭代器
        Iterator<Student> iterator = students.iterator();
        while(iterator.hasNext()){
            Student next = iterator.next();
            System.out.println(next);
        }
        //使用列表迭代器
        ListIterator<Student> studentListIterator = students.listIterator();
        while(studentListIterator.hasNext()){
            Student next = studentListIterator.next();
            System.out.println(next);
        }
        //使用size()和get()方法遍历
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student);
        }
        //使用增强for遍历
        for (Student student : students) {
            System.out.println(student);
        }

    }
}
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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
编写程序,将自定义对象存储到Vector集合,使用泛型并遍历

a) 使用迭代器遍历
b) 使用Vector特有的迭代器遍历
c) 使用size()和get()方法遍历
d) 使用增强for遍历

public class learn1 {
    public static void main(String[] args) {

        Vector<Student> students = new Vector<>();
        students.add(new Student("啊哈",4));
        students.add(new Student("嗯哼",8));
        students.add(new Student("丫丫",4));
        students.add(new Student("二狗子",4));
        students.add(new Student("叮咚",4));
        //使用迭代器
        Iterator<Student> iterator = students.iterator();
        while(iterator.hasNext()){
            Student next = iterator.next();
            System.out.println(next);
        }
        //使用Vector特有的迭代器遍历
        Enumeration<Student> elements = students.elements();
        while(elements.hasMoreElements()){
            Student student = elements.nextElement();
            System.out.println(student);
        }
        //使用size()和get()方法遍历
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student);
        }
        //使用增强for遍历
        for (Student student : students) {
            System.out.println(student);
        }

    }
}
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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

编写程序,将自定义对象存储到LinkedList集合,使用泛型并遍历

a) 使用迭代器遍历
b) 使用列表迭代器遍历
c) 使用size()和get()方法遍历
d) 使用增强for遍历

 public class learn1 {
    public static void main(String[] args) {

        LinkedList<Student> students = new LinkedList<>();
        students.add(new Student("啊哈",4));
        students.add(new Student("嗯哼",8));
        students.add(new Student("丫丫",4));
        students.add(new Student("二狗子",4));
        students.add(new Student("叮咚",4));
        //使用迭代器
        Iterator<Student> iterator = students.iterator();
        while(iterator.hasNext()){
            Student next = iterator.next();
            System.out.println(next);
        }
        //使用列表迭代器遍历
        ListIterator<Student> studentListIterator = students.listIterator();
        while(studentListIterator.hasNext()){
            System.out.println(studentListIterator.next());

        }
        //使用size()和get()方法遍历
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(student);
        }
        //使用增强for遍历
        for (Student student : students) {
            System.out.println(student);
        }

    }
}
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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值