-
概念:对象的容器,定义了对多个对象进行操作的常用方法。可实现数组的功能
-
和数组区别:
-
1.数组长度固定,集合长度不固定
-
2.数组可以存储基本类型和引用类型,集合只能存储引用类型(若想存基本类型可通过装箱操作)
-
-
位置:java.util.*;
Collection接口 Collection体系 Collection父接口
-
特点:代表一组任意类型的对象,无序 、无下标(不可以通过下标来访问fori)、不能重复
-
方法:
-
boolean add(Object obj) //添加一个对象
-
boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中
-
void clear() //清空此集合中的所有对象
-
boolean contains(Object o) //检查此集合中是否包含o对象
-
boolean equals(Object o) //比较此集合是否与指定对象相等
-
boolean isEmpty() //判断此集合是否为空
-
boolean remove(Object o) //在此集合中移除o对象
-
boolean removeAll(Collection c) //移除c和此集合中共有的元素、
-
boolean retainAll(Collection c) //保留c和此集合中共有的元素
-
int size() //返回此集合中的元素个数
-
Object[] toArray() //将此集合转换成数组
-
-
collection使用1
package com.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Application {
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList(); //只能new接口的实现类
//(1)添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("榴莲");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
//(2)删除元素
//collection.remove("榴莲");
//collection.clear();
//System.out.println("删除之后:"+collection.size());
//(3)遍历元素【重点】
//(3.1)使用增强for 不能用for,for需要下标
for (Object o : collection) {
System.out.println(o);
}
System.out.println("----------------------------");
//(3.2)使用迭代器(迭代器专门用来遍历/迭代集合的一种方式)
//iterator方法返回值是Iterator,是一个接口类型
//有三个方法
//hasNext();有没有下一个元素
//next();获取下一个元素,获取完后指令往后移一个
//remove();删除当前元素
Iterator it = collection.iterator();
while(it.hasNext()){
String s = (String)it.next();
System.out.println(s);
//在迭代过程中不允许使用collection的删除方法
//it.remove();
}
System.out.println("元素个数:"+collection.size()); //0
//(4)判断
System.out.println(collection.contains("西瓜")); //true
System.out.println(collection.isEmpty()); //false
}
}
-
collection使用2
package com.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Application {
public static void main(String[] args) {
//新建Collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三",20);
Student s2 = new Student("张无忌",48);
Student s3 = new Student("王二",22);
//1添加数据
collection.add(s1); //把地址存在集合里
collection.add(s2);
collection.add(s3);
System.out.println("元素个数:"+collection.size());
System.out.println(collection.toString());
//2删除数据
//collection.remove(s1);
//collection.clear();
//clear();只是把地址从集合里面删除了,对象仍在堆里
//System.out.println("删除之后:"+collection.size());
//3遍历
//3.1 增强for
for (Object o : collection) {
//为了调用重写的toString方法可以把o强转为Student类
//Student a = (Student) o ;
System.out.println(o);
}
System.out.println("---------------------");
//3.2迭代器 hasNext(); next(); remove(); 迭代过程中不能使用collection的删除方法
Iterator it = collection.iterator();
while(it.hasNext()){
Student s = (Student) it.next();
System.out.println(s);
}
//4判断
System.out.println(collection.contains(s1)); //true
System.out.println(collection.contains(new Student("zhangsan",20))); //false
System.out.println(collection.isEmpty()); //false
}
}
List接口与实现类 List子接口
-
特点:有序、有下标、元素可以重复
-
extends Collection 继承了Collection的所有方法和属性
-
方法:
-
void add(int index,Object o) //在index位置插入对象o
-
boolean addAll(int index,Collection c) //将一个集合中的元素添加到此集合中的index位置
-
Object get(int index) //根据下标返回元素
-
List subList(int fromIndex,int toIndex) //返回fromIndex和toIndex之间的集合元素
-
boolean remove(int index) //删除集合中指定下标的元素
-
int indexOf(object o ) //返回元素的下标
-
-
List子接口使用
package com.collection;
import java.util.*;
public class Application {
public static void main(String[] args) {
//先创建集合对象
List list = new ArrayList();
//1添加元素
list.add("苹果");
list.add("小米");
list.add(0, "华为"); //在第一个位置添加
System.out.println("元素个数:" + list.size());
System.out.println(list.toString()); //[华为, 苹果, 小米]
//2删除元素
//list.remove(“苹果”);
//list.remove(0);
System.out.println("删除之后:" + list.size());
System.out.println(list.toString());
//3遍历 重点
//3.1使用for遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("-------------------------------");
//3.2使用增强for
for (Object o : list) {
System.out.println(o);
}
System.out.println("--------------------------------");
//3.3使用迭代器
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("---------------------------------");
//3.4使用列表迭代器 返回值是一个列表迭代器 是一个接口
//ListIterator extends Iterator
//和Iterator的却别,ListIterator可以向前或向后遍历,添加、删除、修改元素
ListIterator lit = list.listIterator();
//从前往后遍历
while (lit.hasNext()) {
System.out.println(lit.nextIndex()+":"+lit.next()); //打印角标和元素
}
//从后往前遍历
//指针已移到最后一个元素 可直接从后往前遍历
while(lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
System.out.println("-----------------------------------");
//4判断
System.out.println(list.contains("苹果")); //true
System.out.println(list.isEmpty()); //false
//5获取元素位置
System.out.println(list.indexOf("华为")); //0
}
//补充方法subList:返回子集合,含头不含尾
//List subList = list.sbuList(0,2); (1包含3包含)
sout(list.toString()); //[华为,苹果]
}
List实现类
-
ArrayList【重点】:
-
数组结构实现,查询快、增删慢(数据多)
-
JDK1.2版本,运行效率快、线程不安全
-
-
Vector:
-
数组结构实现,查询快、增删慢
-
JDK1.0版本,运行效率慢、线程安全
-
-
linkedList:
-
链表结构实现,增删快、查询慢
-
-
ArrayList使用
package com.collection;
import java.lang.reflect.Array;
import java.util.*;
public class Application {
public static void main(String[] args) {
//创建集合
ArrayList arrayList = new ArrayList();
//1添加元素
Student s1 = new Student("刘德华",20);
Student s2 = new Student("郭富城",22);
Student s3 = new Student("梁朝伟",18);
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
//arrayList.add(s2); 可重复且有序
System.out.println("元素个数:"+arrayList.size());
System.out.println(arrayList.toString());
//2删除元素
//arrayList.remove(new Student("刘德华",20));
//remove实际上调用equals方法(this==obj)地址进行比较,new的元素不在集合里面所以删不掉
//从object里继承的方法不满足需求--->重写equals方法
//重写之后可以删除(比较的是内容而不是地址)
//arrayList.remove(s1);
//System.out.println("删除之后:"+arrayList.size());
//3遍历元素【重点】
//3.1使用迭代器
Iterator it = arrayList.iterator();
while(it.hasNext()){
Student s = (Student)it.next(); //可强转成Student类
System.out.println(s.toString());
}
//3.2列表迭代器
ListIterator lit = arrayList.listIterator();
while(lit.hasNext()){
System.out.println(lit.next());
}
while(lit.hasPrevious()){
System.out.println(lit.previous());
}
//4判断
System.out.println(arrayList.contains(s1)); //true
System.out.println(arrayList.contains(new Student("梁朝伟",18))); //true
//结果为ture,contains调用了equals方法,比较的是内容
System.out.println(arrayList.isEmpty()); //false
//5查找
System.out.println(arrayList.indexOf(new Student("梁朝伟",18)));
//结果为2 indexOf也调用了equals方法
}
}
-
ArrayList源码分析
-
DEFAULT_CAPACITY=10 默认容量大小
-
注意:如果没有向集合中添加任何元素时,容量为0 无参构造:this.elementData=空数组;添加一个元素之后,容量为10,每次扩容大小是原来的1.5倍
-
elementData 存放元素的数组
-
size 实际的元素个数
-
创建集合 size 0 容量0 add() 超过10,扩容为原来的1.5倍
-
-
Vector使用
-
使用枚举器遍历
Enumeration en = vector.elements(); while(en.hasMoreElements()){ System.out.println(en.nextElement()); }- vector其他方法:firstElement();lastElement();ElementAt();
-
-
linkedList源码分析
-
int size 集合的大小
-
Node first 链表的头节点
-
Node last 链表的尾节点
-
泛型和工具类 泛型概述
-
Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递
-
常见形式有泛型类、泛型接口、泛型方法
-
语法:
-
T称为类型占位符,表示一种引用类型
-
-
好处:
-
(1)提高代码的重用性
-
(2)防止类型转换异常,提高代码的安全性
-
泛型类
package com.collection; public class MyGerneric{ //使用泛型T //1创建变量 T t; //不能实例化T,因为不确定T的数据类型,不能保证传过来的类型的构造方法一定能用 //T t = new T(); 错 //2泛型作为方法的参数 public void show(T t){ System.out.println(t); } //3泛型作为方法的返回值 public T getT(){ return t; } } package com.collection; public class TestGerneric { public static void main(String[] args) { //使用泛型类创建对象 //给一个实际的引用数据类型不能用占位符 //注意,1泛型只能使用引用类型 2不同泛型类型对象之间不能相互赋值 MyGerneric myGerneric = new MyGerneric<>(); myGerneric.t="hello"; myGerneric.show("大家好,加油"); String string = myGerneric.getT(); MyGerneric myGerneric2 = new MyGerneric<>(); myGerneric2.t=100; myGerneric2.show(200); Integer integer = myGerneric2.getT(); //MyGerneric myGerneric3 = myGerneric2; 错 //MyGerneric myGerneric3 = myGerneric; 对 } }
泛型接口
-
package com.collection; public interface MyInterface
{ String name = "张三"; //不知道啥类型 //T t = new T(); 错误 T server(T t); } -
两种使用方式:1写实现类的时候直接确定类型(用之前确定类型) 2把实现类也变成泛型类(用时确定类型)
-
方式1(用之前确定类型)
package com.collection; public class MyInterfaceImpl implements MyInterface{ @Override public String server(String s) { System.out.println(s); return s; } } test: MyInterfaceImpl impl = new MyInterfaceImpl(); impl.server("xxxxxx");
-
方式2(用时确定类型)
package com.collection; public class MyInterfaceImpl2implements MyInterface { @Override public T server(T t) { return null; } } test: MyInterfaceImpl2 impl2 = new MyInterfaceImpl2<>(); impl2.server(1000);
泛型方法
package com.collection;
public class MyGenericMethod {
//泛型方法
public T show(T t){
System.out.println("泛型方法"+t);
return t;
}
}
test:
//调用泛型方法
//实现方法的重用性
//1个方法可以传递任何类型的数据
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show("中国加油"); //根据传递的数据来确定
myGenericMethod.show(200);
myGenericMethod.show(3.14);
泛型集合
-
概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致
-
特点:
-
编译时即可检查,而非运行时抛出异常
-
访问时,不必类型转换(拆箱)
-
不同泛型之间引用不能相互赋值,泛型不存在多态
-
package com.collection;
import java.util.ArrayList;
import java.util.Iterator;
public class Demo03 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList<>();
arrayList.add("xxx");
arrayList.add("yyy");
//arrayList.add(10); //编译出错,必须要String类型
//arrayList.add(20);
for (String s : arrayList) {
System.out.println(s);
}
ArrayList arrayList2 = new ArrayList<>();
Student s1 = new Student("刘德华",20);
Student s2 = new Student("郭富城",22);
Student s3 = new Student("梁朝伟",18);
arrayList2.add(s1);
arrayList2.add(s2);
arrayList2.add(s3);
//arrayList2.add(100); //错误,必须是Student类型
Iterator it = arrayList2.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
Set接口与实现类 Set子接口
-
特点:无序、无下标、元素不可重复
-
方法:全部继承自Collection的方法
-
set接口使用
package com.collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
//创建集合
Set set = new HashSet<>();
//1添加数据
set.add("小米");
set.add("苹果");
set.add("华为");
System.out.println("数据个数:"+set.size());
System.out.println(set.toString());
//2删除数据
//set.remove("小米");
//3遍历【重点】
//3.1增强for
for (String s : set) {
System.out.println(s);
}
System.out.println("-------------------------");
//3.2使用迭代器
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//4判断
System.out.println(set.contains("华为"));
System.out.println(set.isEmpty());
}
}
Set实现类
-
HashSet【重点】:
-
基于HashCode计算元素存放位置
-
当存入元素的哈希码相同时,会调用equals进行确认,如结果为true,则拒绝后者存入
-
哈希表:0-4换成数组上面 下面排了一列人
-
-
HashSet使用
package com.collection;
import java.util.HashSet;
public class Demo02 {
public static void main(String[] args) {
//创建集合
HashSet hashSet = new HashSet<>();
hashSet.add("刘德华");
hashSet.add("梁朝伟");
hashSet.add("林志玲");
hashSet.add("周润发");
System.out.println("元素个数:"+hashSet.size());
System.out.println(hashSet.toString());
//2删除数据
//hashSet.remove("刘德华");
//3遍历操作
//3.1增强for
//3.2使用迭代器
//4判断
}
}
-
HashSet存储过程/重复依据
-
根据hashcode计算保存的位置(数组中的位置),如果此位置为空,则直接保存,如果不为空执行第二步
-
再执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表
-
存储过程/重复依据!!!!!!
package com.collection;
import java.util.HashSet;
public class Demo04{
public static void main(String[] args) {
Student s1 = new Student("梁朝伟", 18);
Student s2 = new Student("刘德华", 20);
Student s3 = new Student("张学友", 22);
//实现add(new Student("梁朝伟",18))与s1是重复的;
//add方法是根据hashCode和equals方法
HashSet student = new HashSet<>();
student.add(s1);
student.add(s2);
student.add(s3);
student.add(new Student("梁朝伟",18));
System.out.println("元素个数:"+student.size());
System.out.println(student.toString());
//没有重写hashCode方法之前把new的也加进来了
//重写了hashcode方法之后
//new的和s1的hashcode是一样的,计算的位置是同一个,执行第二步
//因为equals没有重写,所以new的和s1还不是同一个对象 实际上形成了一个链表
//new的接在了s1后面形成链表
//重写了hashcode方法和equals方法之后
//new的和s1是同一个对象了
//所以new的不能加入集合里面了,因为new和s1是同一个对象,new和s1重复了,List集合是不重复的,s1已在集合里面了
//结论:要重复的话重写equals和hashCode方法
student.remove(new Student("梁朝伟",18))
//也能删掉s1,因为remove也是根据hashCode和equals找的元素,s1和new的是同一个对象
//hashCode的方法都是根据hashCode和equals
}
}
-
TreeSet:
-
基于排列顺序实现元素不重复
-
实现了SortedSet接口,对集合元素自动排序
-
元素对象的类型必须实现Comparable接口,指定排序规则
-
通过CompareTo方法确定是否为重复元素
-
红黑树是一个二叉查找树,一个结点最多只有两个子结点,添加的第一个元素为根结点,左边的结点元素都比右边的要小
红黑树在二叉查找树的基础上加点颜色,根结点必须是黑的,所有的nil结点(叶子结点)都是黑的
-
TreeSet使用
package com.collection;
import java.util.TreeSet;
public class Demo05 {
public static void main(String[] args) {
//创建集合
TreeSet student = new TreeSet<>();
//1添加元素
Student s1 = new Student("xyz", 18);
Student s2 = new Student("hello", 20);
Student s3 = new Student("zhangsan",22);
Student s4 = new Student("zhangsan",20);
//有两个zhangsan 20的在前面 22在后面
student.add(s1);
student.add(s2);
student.add(s3);
student.add(s4);
System.out.println("元素个数:"+student.size());
//.Student cannot be cast to class java.lang.Comparable
//TreeSet是红黑树,为了保证小的在左边大的在右边需要进行比较,但是没有一个比较的标准,不知道比啥
//所以出现了异常,所以得告诉集合怎么来比
//为了把Student对象添加到TreeSet集合当中,必须要实现Comparable接口
//2删除
student.remove(new Studnet("zhangsan",22));
//可以删除,因为比较规则是姓名和年龄
System.out.println(student.toString());
//Student s1 = new Student("xyz", 18);
//Student s2 = new Student("hello", 20);
//Student s3 = new Student("zhangsan", 22);
//比较结果是hello,xyz,zhangsan
}
}
比较规则:
//写比较规则
//先按姓名比,然后按年龄比
@Override
public int compareTo(Student o) {
//先比姓名
//拿当前对象的名字和传递的o的名字来比
int n1=this.getName().compareTo(o.getName());
//比年龄
int n2=this.age-o.getAge();
return n1==0?n2:n1; //返回值是0认为重复
}
-
Comparator接口:实现定制比较
package com.collection;
import com.sun.source.tree.Tree;
import java.util.Comparator;
import java.util.TreeSet;
public class Demo05 {
public static void main(String[] args) {
//创建集合,并指定比较规则
TreeSet student = new TreeSet<>(new Comparator() {
//比较规则
//先比较年龄再比较姓名
@Override
public int compare(Student o1, Student o2) {
int n1=o1.getAge()-o2.getAge();
int n2=o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
}
}
package com.collection;
import com.sun.source.tree.Tree;
import java.util.Comparator;
import java.util.TreeSet;
public class Demo05 {
public static void main(String[] args) {
//创建集合,并指定比较规则
TreeSet str = new TreeSet<>(new Comparator() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length() - o2.length();
int n2 = o1.compareTo(o2);
return n1 == 0 ? n2 : n1;
}
});
str.add("helloworld");
str.add("pingguo");
str.add("zhangsan");
str.add("lisi");
str.add("beijing");
System.out.println("元素个数:"+str.size());
System.out.println(str.toString());
//[lisi, beijing, pingguo, zhangsan, helloworld]
//先长度,再字典表
}
}
Map接口与实现类 Map体系集合
Map父接口
-
特点:存储一对数据(Key-Value),无序、无下标,键不可重复,值可重复
-
方法:
-
V put(K key,V value) //将对象存入到集合中,关联键值。key重复则覆盖原值
-
Object get(Object key) //根据键获取对应的值
-
keySet
//返回所有Key的Set集合,搭配get使用 -
Collection
values() //返回包含所有值的Collection集合 -
Set
> entrySet > //键值匹配的Set集合,返回Key和Value,搭配getKey和getValue使用 -
V get(Object key) //根据key来获取value
-
boolean remove(Object key)
-
entrySet()
-
-
Map接口使用
package com.collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo06 {
public static void main(String[] args) {
//创建Map集合
Map map = new HashMap<>();
//1添加元素
map.put("cn","中国");
map.put("uk","英国");
map.put("usa","美国");
map.put("cn","zhongguo");
System.out.println("元素个数:"+map.size());
System.out.println(map.toString()); //Key重复覆盖原值
//2删除
//map.remove("cn");
//ystem.out.println("删除之后:"+map.size());
//3遍历
//3.1使用keySet(); 返回值是Set集合
//Set keyset = map.keySet();
//for (String s : keyset) {
// System.out.println(s+"---"+map.get(s));
//} //加上get方法得到key和value
//合在一起写
for (String s : map.keySet()) {
System.out.println(s+"---"+map.get(s));
}
//3.2使用entryMap()方法
//返回值是一个Set集合
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
//4判断
System.out.println(map.containsKey("cn")); //true
System.out.println(map.containsValue("泰国")); //false
}
}
-
Map集合的实现类
-
HashMap【重点】:
-
JDK1.2版本,线程不安全,运行效率快;允许用null作为key或是value
-
-
Hashtable:
-
JDK1.0版本,线程安全,运行效率慢;不允许null作为key或是value
-
-
Properties:
-
Hashtable的子类,要求key和value都是String。通常用于配置文件的读取
-
-
TreeMap:
-
实现了SortedMap接口(是Map的子接口),可以对Key自动排序
-
-
HashMap使用
package com.collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class Demo07 {
public static void main(String[] args) {
//创建集合
HashMap students = new HashMap<>();
//添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",101);
Student s3 = new Student("沙和尚",102);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"杭州");
//students.put(s3,"南京");
students.put(new Student("沙和尚",102),"杭州"); //能加入集合中
//重写了hashCode和equals方法之后就加不进来
System.out.println("元素个数:"+students.size());
System.out.println(students.toString());
//2删除
//students.remove(s1);
//System.out.println(students.toString());
//3遍历
//3.1使用KeySet();
for (Student key : students.keySet()) {
System.out.println(key.toString()+"==="+students.get(key));
}
//3.2使用entrySet();
for (Map.Entry entry : students.entrySet()) {
System.out.println(entry.getKey()+"==="+entry.getValue());
}
//4判断
System.out.println(students.containsKey(s1)); //true
System.out.println(students.containsKey(new Student("沙和尚",102))); //true 因为重写了hashCode和equals方法
System.out.println(students.containsValue("杭州")); //true
System.out.println(students.isEmpty()); //false
}
}
-
源码分析
TreeMap使用
package com.collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeMap;
public class Demo07 {
public static void main(String[] args) {
//新建集合,必须实现Comparable接口或者定制比较
TreeMap treeMap = new TreeMap<>();
//1添加元素
Student s1 = new Student("孙悟空",100);
Student s2 = new Student("猪八戒",101);
Student s3 = new Student("沙和尚",102);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"深圳");
treeMap.put(new Student("沙和尚",102),"南京");
//不能加进来,因为比较的依据是CompareTo,如果stuNo学号相同,则认为重复,加不进来
//"南京"把原来的"深圳"代替了
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
}
}
比较规则:
@Override
public int compareTo(Student o) {
int n2 =this.stuNo-o.getStuNo();
return n2;
}
Collections工具类
-
概念:集合工具类,定义了除了存取以外的集合常用方法
-
方法:
-
public static void reverse(List> list) //反转集合中元素的顺序
-
public static void shuffle(List> list) //随机重置集合元素的顺序
-
public static void sort(List> list) //升序排序(元素类型必须实现Comparable接口)
-
package com.collection;
import java.lang.reflect.Array;
import java.security.CodeSigner;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
public class Application {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(20);
list.add(5);
list.add(12);
list.add(30);
list.add(6);
//sort排序
System.out.println("排序之前:"+list.toString()); //排序之前:[20, 5, 12, 30, 6]
Collections.sort(list);
System.out.println("排序之后:"+list.toString()); //排序之后:[5, 6, 12, 20, 30]
//binarySearch二分查找
int i=Collections.binarySearch(list,12); //找12结果为2,找13结果为负
System.out.println(i); //正找到负没找到
//copy复制
List dest = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
dest.add(0);
} //保证大小和list一样
Collections.copy(dest,list);
System.out.println(dest.toString()); //[5, 6, 12, 20, 30]
//reverse反转
Collections.reverse(list);
System.out.println("反转之后:"+list.toString()); //反转之后:[30, 20, 12, 6, 5]
//shuffle打乱
Collections.shuffle(list);
System.out.println("打乱之后:"+list.toString()); //打乱之后:[30, 12, 5, 20, 6]
//补充:list转成数组
Integer[] arr = list.toArray(new Integer[0]);
System.out.println(arr.length); //5
System.out.println(Arrays.toString(arr)); //[5, 12, 20, 6, 30]
//数组转成集合
String[] names = {"张三","李四","王五"};
//数组转成集合,这个集合是一个受限集合,不能添加和删除,因为数组长度固定
List list2 = Arrays.asList(names);
//list2.add("111"); Exception
System.out.println(list2); //[张三, 李四, 王五]
//把基本类型数组转成集合时,需要修改为包装类型
}
} 


