第十三话-Java集合框架

集合

一:集合概念

对象的容器,实现了对多个对象进行操作的常用方法。可实现数组的功能。

和数组区别:

【1】数组长度固定,集合长度不固定

【2】数组可以存储基本类型和引用类型,集合只能存储引用类型(基本类型装箱)

位置:java.util.*

二、Collection体系集合

Set接口特点:无序、无下标、元素不可重复

三、Map集合

  • 用于存储任意键值对(Key-Value)
  • 键:无序、无下标、不允许重复
  • 值:无序、无下标、允许重复

Collection父接口

常用方法如下,还有很多其他方法,详见API

package com.xie.collection;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo01 {
    public static void main(String[] args) {
        //创建集合
        Collection collection = new ArrayList();
        //添加元素
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        System.out.println(collection.size() + "个元素" + collection.toString());
        //删除元素
        collection.remove("苹果");//删除一个元素
        System.out.println(collection.size() + "个元素" + collection.toString());
        collection.clear();//删除全部元素
        System.out.println(collection.size() + "个元素" + collection.toString());
        //遍历元素,1.增强for
        collection.add("苹果");
        collection.add("西瓜");
        collection.add("葡萄");
        for (Object object:collection){
            System.out.println(object);
        }
        //遍历元素,1.迭代器
        Iterator it = collection.iterator();
        while (it.hasNext()){
            Object object = it.next();
            System.out.println(object);
            //collection.remove(object);迭代过程中不能使用collection方法
            it.remove();
        }
        System.out.println(collection.size() + "个元素" + collection.toString());
        //判断
        collection.add("苹果");
        collection.add("西瓜");
        System.out.println(collection.contains("西瓜"));
        System.out.println(collection.isEmpty());
    }
}
package com.xie.collection;

import javax.swing.text.html.HTMLDocument;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo02 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        Student s1 = new Student("张三",12);
        Student s2 = new Student("李四",13);
        collection.add(s1);
        collection.add(s2);
        System.out.println(collection);
        //删除
        collection.remove(s1);
        System.out.println(collection );
        collection.clear();
        System.out.println(collection);
        //遍历,增强for
        collection.add(s1);
        collection.add(s2);
        for(Object object : collection){
            System.out.println(object);
        }
        //遍历,迭代器
        Iterator it = collection.iterator();
        while (it.hasNext()){
            Object object = it.next();
            System.out.println(object);
        }
        //判断
        System.out.println(collection.isEmpty());
        System.out.println(collection.contains(s1));
    }
}

class Student{
    private String name;
    private int age;

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

List子接口

List接口特点:有序、有下标、元素可重复

常用方法如下:

package com.xie.collection;

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

public class Demo03 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("华为");
        list.add("小米");
        //普通for
        System.out.println("------普通for------");
        for(int i = 0; i<list.size(); i++){
            System.out.println(list.get(i));
        }
        //增强for
        System.out.println("------增强for------");
        for (Object object : list){
            System.out.println(object);
        }
        //迭代器
        System.out.println("------迭代器------");
        Iterator it = list.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        //列表迭代器,可以向前或向后添加、删除、修改、遍历元素
        ListIterator lit = list.listIterator();
        //从前往后遍历
        System.out.println("------列表迭代器从前往后遍历------");
        while (lit.hasNext()){
            System.out.println(lit.nextIndex() + "" +lit.next());
        }
        //从后往前遍历
        System.out.println("------列表迭代器从后往前遍历------");
        while (lit.hasPrevious()){
            System.out.println(lit.previousIndex() + "" +lit.previous());
        }
        //判断
        System.out.println(list.contains("小米"));
        System.out.println(list.isEmpty());

        //获取位置
        System.out.println(list.indexOf("小米"));
    }
}
package com.xie.collection;

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

public class Demo04 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(20);//自动装箱,其实添加的是Integer类型
        list.add(30);
        list.add(40);
        list.add(50);
        list.add(60);
        System.out.println(list.size()+"个元素:"+list);
        list.remove((Object) 20);
        //list.remove(0);
        //list.remove(new Integer(20));
        System.out.println(list.size()+"个元素:"+list);
        list.add(0,20);
        System.out.println(list.size()+"个元素:"+list);
        List subList = list.subList(1,3);//含头不含尾
        System.out.println(subList.size()+"个元素:"+subList);
    }
}

List实现类

  1. ArrayList:
  • 数组结构实现,查询快、增删慢;

  • 运行效率高、线程不安全

  1. Vector:
  • 数组结构实现,查询快、增删慢;

  • 运行效率比ArrayList慢、线程安全

  1. LinkedList:
  • 链表结构实现,增删快、查询慢;

ArrayList类

  • 默认容量大小为10,DEFAULT_CAPACITY = 10。如果没向集合中添加任何元素时,容量0
  • 存放元素的数组:Object[] elementData
  • size:实际元素个数
package com.xie.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class Demo05 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        Student s1 = new Student("刘德华",33);
        Student s2 = new Student("张学友",31);
        Student s3 = new Student("周杰伦",32);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        System.out.println(arrayList.size()+"个元素"+arrayList);
        //默认删除时,会比较内存地址,所以new的实例不会删除,如果重写Student的equals方法,就可以删除
        arrayList.remove(new Student("刘德华",33));
        System.out.println(arrayList.size()+"个元素"+arrayList);
        //迭代器
        System.out.println("-------------迭代器-----------");
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        //列表迭代器
        System.out.println("-------------迭代器从前往后-----------");
        ListIterator lit = arrayList.listIterator();
        while (lit.hasNext()){
            System.out.println(lit.nextIndex()+":"+lit.next());
        }
        System.out.println("-------------迭代器从后往前-----------");
        while (lit.hasPrevious()){
            System.out.println(lit.previousIndex()+":"+lit.previous());
        }
        //判断
        System.out.println(arrayList.isEmpty());
        //重写equals方法后,判断为true
        System.out.println(arrayList.contains(new Student("张学友",31)));

        //查找
        System.out.println(arrayList.indexOf(s3));
    }
}
package com.xie.collection;

import java.util.Objects;

public class Student {
    private String name;
    private int age;

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

}

Vector类

package com.xie.collection;

import java.util.Enumeration;
import java.util.Vector;

public class Demo06 {
    public static void main(String[] args) {
        Vector vector = new Vector();
        vector.add("草莓");
        vector.add("苹果");
        vector.add("橙子");
        System.out.println(vector.size()+"个元素"+vector);
        vector.remove(0);
        //vector.remove("苹果");
        //vector.clear();
        System.out.println(vector.size()+"个元素"+vector);
        //遍历,使用枚举器
        Enumeration enumeration = vector.elements();
        while (enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
        System.out.println(vector.contains("橙子"));
        System.out.println(vector.isEmpty());
        System.out.println(vector.firstElement());
        System.out.println(vector.lastElement());
        System.out.println(vector.get(0));
    }
}

LinkedList类

package com.xie.collection;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Demo07 {
    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        Student s1 = new Student("刘德华",33);
        Student s2 = new Student("张学友",31);
        Student s3 = new Student("周杰伦",32);
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        System.out.println(linkedList.size()+"个元素"+linkedList);
        linkedList.remove(new Student("周杰伦",32));
        System.out.println(linkedList.size()+"个元素"+linkedList);
        for(int i=0;i<linkedList.size();i++){
            System.out.println(linkedList.get(i));
        }
        for(Object object : linkedList){
            System.out.println(object);
        }
        Iterator it = linkedList.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        ListIterator lit = linkedList.listIterator();
        while (lit.hasNext()){
            System.out.println(lit.next());
        }
        System.out.println(linkedList.isEmpty());
        System.out.println(linkedList.contains(s1));
        System.out.println(linkedList.get(0));
    }
}

泛型

形式:泛型类、泛型接口、泛型方法。

语法:<T,...> T称为类型占位符,表示一种引用类型。也可以是E、K、V标识

好处:1.提高代码的重用性

​ 2.防止类型转换异常,提供代码的安全性。

泛型类

package com.xie.collection;

public class Demo08 {
    public static void main(String[] args) {
        MyGeneric<String> myGeneric = new MyGeneric<String>();
        myGeneric.t = "hello";
        myGeneric.show("加油");
        String string = myGeneric.getT();
        System.out.println(string);

        MyGeneric<Integer> myGeneric2 = new MyGeneric<Integer>();
        myGeneric2.t = 100;
        myGeneric2.show(200);
        System.out.println(myGeneric2.getT());
    }
}

package com.xie.collection;

/**
 * 语法:类名<t>
 * T是类型占位符,表示一种引用类型,如果编写多个,使用逗号隔开
 * @param <T>
 */
public class MyGeneric<T> {
    //使用泛型T,创建变量
    T t;
    //作为方法的参数
    public void show(T t){
        System.out.println(t);
    }
    //作为方法的返回值
    public T getT(){
        return t;
    }
}

泛型接口

package com.xie.collection;

public interface MyGenericInterface<T> {
    //不能定义泛型静态常量
    String name = "张三";

    T server(T t);
}

package com.xie.collection;

public class MyGenericInterfaceImpl implements MyGenericInterface<String>{

    @Override
    public String server(String s) {
        return s;
    }
}
package com.xie.collection;

public class MyGenericInterfaceImpl2<T> implements MyGenericInterface<T>{
    @Override
    public T server(T t) {
        return t;
    }
}
package com.xie.collection;

public class Demo09 {
    public static void main(String[] args) {
        MyGenericInterfaceImpl mgii = new MyGenericInterfaceImpl();
        System.out.println(mgii.server("xiexie"));

        MyGenericInterfaceImpl2<Integer> m2 = new MyGenericInterfaceImpl2<Integer>();
        System.out.println(m2.server(200));
    }
}

泛型方法

package com.xie.collection;

public class MyGenericMethod {
    public <T> T show(T t){
        System.out.println("泛型方法"+t);
        return t;
    }
}
package com.xie.collection;

public class Demo09 {
    public static void main(String[] args) {
        MyGenericMethod myGenericMethod = new MyGenericMethod();
        String s = myGenericMethod.show("xxx");
        System.out.println(s);
        myGenericMethod.show(100);
    }
}

泛型集合

概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致

特点:

  • 编译时即可检测,而非运行时抛出异常

  • 访问时,不必类型转换(拆箱)

  • 不同泛型之间引用不能相互赋值,泛型不存在多态

package com.xie.collection;

import com.xie.oop.demo03.A;

import java.util.ArrayList;
import java.util.Iterator;

public class Demo10 {
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        arrayList.add("xxx");
        arrayList.add(10);
        System.out.println(arrayList);
        //运行报错
//        for(Object object:arrayList){
//            String str = (String) object;
//            System.out.println(str);
//        }
        ArrayList<String> arrayList2 = new ArrayList<String>();
        arrayList2.add("xxx");
        arrayList2.add("10");
        for(String str:arrayList2){
            System.out.println(str);
        }

        ArrayList<Student> arrayList3 = new ArrayList<Student>();
        arrayList3.add(new Student("zhang",33));
        arrayList3.add(new Student("li",331));
        arrayList3.add(new Student("wang",133));
        Iterator<Student> it = arrayList3.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}

Set子接口

特点:无序、无下标、元素不可重复

方法:全部继承自Collection中的方法

package com.xie.collection;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Demo11 {
    public static void main(String[] args) {
        Set<String > set = new HashSet<>();
        set.add("华为");
        set.add("小米");
        set.add("OPPO");
        set.add("OPPO");
        System.out.println(set.size()+"个元素"+set);
        set.remove("OPPO");
        System.out.println(set.size()+"个元素"+set);
        for (String string : set){
            System.out.println(string);
        }
        Iterator<String> it = set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println(set.isEmpty());
        System.out.println(set.contains("小米"));
    }
}

HashSet类

  • 基于HashCode计算元素存放位置
  • 当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝存入。
package com.xie.collection;

import java.util.HashSet;
import java.util.Iterator;

public class Demo12 {
    public static void main(String[] args) {
        HashSet<Student> students = new HashSet<>();
        students.add(new Student("zhang",11));
        students.add(new Student("wang",12));
        students.add(new Student("li",21));
        //重写hashCode和equals方法,就不能加进去
        students.add(new Student("li",21));
        System.out.println(students.toString());
        students.remove(new Student("wang",12));
        System.out.println(students);
        for(Student student : students){
            System.out.println(student);
        }
        Iterator<Student> it = students.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println(students.isEmpty());
        System.out.println(students.contains(new Student("li",21)));
    }
}

package com.xie.collection;

import java.util.Objects;

public class Student {
    private String name;
    private int age;

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

TreeSet类

  • 基于排列排序实现元素不重复
  • 实现了SortedSet接口,对集合元素自动排序
  • 元素对象的类型必须实现Comparable接口,指定排序规则
  • 通过CompareTo方法确定是否为重复元素
package com.xie.collection;

import java.util.Iterator;
import java.util.TreeSet;

public class Demo13 {
    public static void main(String[] args) {
        TreeSet<Student> students = new TreeSet<>();
        Student student1 = new Student("zhangsan",13);
        Student student2 = new Student("aabc",22);
        Student student3 = new Student("xyc",15);
        Student student4 = new Student("xyc",22);
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
        System.out.println(students);
        students.remove(new Student("xyc",15));
        for(Student s : students){
            System.out.println(s);
        }
        Iterator<Student> it = students.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println(students.contains(new Student("zhangsan",13)));
    }
}
package com.xie.collection;

import java.util.Objects;

public class Student implements Comparable<Student>{
    private String name;
    private int age;

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Student o) {
        int n1 = this.name.compareTo(o.name);
        int n2 = this.age - o.age;
        return n1==0?n2:n1;
    }
}

通过实现Comparator接口(比较器),也可以实现定制比较。

package com.xie.collection;

import java.util.Comparator;
import java.util.TreeSet;

public class Demo14 {
    public static void main(String[] args) {
        TreeSet<Person> persons = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                int n1 = o1.getAge() - o2.getAge();
                int n2 = o1.getName().compareTo(o2.getName());
                return n1==0?n2:n1;
            }
        });
        persons.add(new Person("xyc",22));
        persons.add(new Person("aaxyac",25));
        persons.add(new Person("zzzxyac",20));
        persons.add(new Person("ccccc",20));
        System.out.println(persons);
    }
}

package com.xie.collection;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", 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;
    }
}

Map父接口

  • 特点:存储一对数据(Key-Value),无序、无下标、键不允许重复,值允许重复

  • Map常用方法:

package com.xie.collection;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Demo15 {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("cn","中国");
        map.put("uk","英国");
        map.put("usa","美国");
        map.put("cn","china");
        System.out.println(map.size()+"个元素"+map.toString());
        //删除
        map.remove("usa");
        System.out.println(map.size()+"个元素"+map.toString());
        //keySet()遍历
        //Set<String> keySet= map.keySet();
        for (String key : map.keySet()) {
            System.out.println(key+"---"+map.get(key));
        }
        //entrySet()遍历
        System.out.println("========entrySet==========");
        //Set<Map.Entry<String,String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey()+"----"+entry.getValue());
        }
    }
}

HashMap类

  • 线程不安全,运行效率快,允许使用null作为key或value
  • Hashtable:线程安全,运行效率慢;不允许null作为key或value
  • Properties:Hashtable的子类,要求key和value都是String,通常用于配置文件的读取
package com.xie.collection;

import java.util.HashMap;
import java.util.Map;

public class Demo16 {
    public static void main(String[] args) {
        HashMap<Person,String> persons = new HashMap<>();
        Person p1 = new Person("张三",33);
        Person p2 = new Person("李四",22);
        Person p3 = new Person("王五",34);
        persons.put(p1,"北京");
        persons.put(p2,"上海");
        persons.put(p3,"天津");
        System.out.println(persons.size()+"个元素"+persons.toString());
        //如果key的Person类重写hashCode和equals方法,则会判断为重复
        persons.put(new Person("王五",34),"南津");
        System.out.println(persons.size()+"个元素"+persons.toString());
        persons.remove(p1);
        //keySet()遍历
        for(Person key:persons.keySet()){
            System.out.println(key.toString()+"----"+persons.get(key));
        }
        System.out.println("========entrySet==========");
        for(Map.Entry<Person,String> entry:persons.entrySet()){
            System.out.println(entry.getKey()+"---"+entry.getValue());
        }
        System.out.println(persons.containsKey(new Person("李四",22)));
        System.out.println(persons.containsValue("南津"));
    }
}
package com.xie.collection;

import java.util.Objects;

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", 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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

TreeMap类

实现了SortedMap接口,可以对key自动排序

package com.xie.collection;

import java.util.Map;
import java.util.TreeMap;

public class Demo17 {
    public static void main(String[] args) {
        //Teacher必须实现Comparable<Teacher>接口,重写compareTo方法
        TreeMap<Teacher,String> teachers = new TreeMap<>();
        Teacher t1 = new Teacher("wangwu",53);
        Teacher t2 = new Teacher("zhangsan",33);
        Teacher t3 = new Teacher("lisi",44);
        teachers.put(t1,"天津");
        teachers.put(t2,"杯津");
        teachers.put(t3,"天津");
        System.out.println(teachers);
        teachers.remove(new Teacher("lisi",44));
        System.out.println(teachers);
        for(Teacher t: teachers.keySet()){
            System.out.println(t+"--"+teachers.get(t));
        }
        System.out.println("=====entrySet=====");
        for(Map.Entry<Teacher,String > entry:teachers.entrySet()){
            System.out.println(entry.getKey()+"--"+entry.getValue());
        }
        System.out.println(teachers.containsKey(t2));
    }
}
package com.xie.collection;

public class Teacher implements Comparable<Teacher>{
    private String name;
    private int 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;
    }

    public Teacher(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    @Override
    public int compareTo(Teacher o) {
        int n = this.age - o.getAge();
        return n;
    }
}

Collections工具类

package com.xie.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Demo18 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(20);
        list.add(5);
        list.add(30);
        list.add(311);
        System.out.println(list);
        //排序
        Collections.sort(list);
        System.out.println(list);
        //二分查找
        int i =Collections.binarySearch(list, 11);
        System.out.println(i);
        //copy复制,dest必须和list长度一样,否则报错
        List<Integer> dest = new ArrayList<>();
        for (int i1 = 0; i1 < list.size(); i1++) {
            dest.add(0);
        }
        Collections.copy(dest,list);
        System.out.println(dest);
        //反转
        Collections.reverse(list);
        System.out.println(list);
        //打乱
        Collections.shuffle(list);
        System.out.println(list);

        //list转成数组
        Integer[] arr = list.toArray(new Integer[6]);
        System.out.println(arr.length+"长度,"+ Arrays.toString(arr));
        //数组转成list,是一个受限集合,不能添加和删除
        String[] names = {"张三","李四","王五"};
        List<String> list2 = Arrays.asList(names);
        //list2.add("ceshi");//执行报错
        System.out.println(list2);

        //基本数据类型定义的数组转换后的list,元素类型是int[],不能自动变成Integer,需使用包装类
        //int[] nums = {100,200,300,400};
        //List<int[]> list3 = Arrays.asList(nums);
        Integer[] nums2 = {100,200,300,400};
        List<Integer> list4 = Arrays.asList(nums2);
    }
}
posted @   少年阿川  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示