集合
集合概念:
对象的容器,实现了对对象常用的操作,类似数组功能。
和数组的区别:
-
数组长度固定,集合长度不固定;
-
数组可以存储基本类型和引用类型,集合只能存储引用类型。
位置:
java.util.*;
Collection接口
Collection接口总体框架:
Collection接口的使用01
-
添加元素:add();
-
删除元素:remove();
-
遍历元素:增强for/迭代器
-
判断:contain(); isEmpty();
//创建集合 Collection collection = new ArrayList(); //1.添加元素 collection.add("A"); collection.add("B"); collection.add("C"); System.out.println(collection.size()); //2.删除元素 collection.remove("B"); System.out.println(collection.size()); //3.遍历 //(1)增强for遍历 // for (Object object:collection){ // System.out.println(object); // } //(2)迭代器遍历(Iterator迭代器是专门用来遍历集合的一种方式) //hasNext();next();remove(); Iterator it = collection.iterator(); while (it.hasNext()){ Object next = it.next(); System.out.println(next); // it.remove(); } System.out.println(collection.size()); //4.判断 System.out.println(collection.contains("A")); System.out.println(collection.isEmpty());
Collection接口的使用02
Collection collection = new ArrayList();
Student s1 = new Student("提莫",18);
Student s2 = new Student("小鱼人",13);
//1.添加数据
collection.add(s1);
collection.add(s2);
System.out.println("元素个数"+collection.size());
System.out.println(collection.toString());
//2.删除元素
// collection.remove(s1);
collection.remove(new Student("提莫",18));
System.out.println("删除之后元素个数"+collection.size());
// collection.clear();
System.out.println(collection.toString());
//3.遍历元素
//(1)增强for
for (Object object: collection
) {
Student student = (Student) object;
System.out.println(student.toString());
}
//(2)迭代器Iterator:hasNext();next();remove()
Iterator it = collection.iterator();
while (it.hasNext()){
Student ss = (Student) it.next();
System.out.println(ss.toString());
}
//4.判断
System.out.println(collection.contains(s2));
System.out.println(collection.isEmpty());
List子接口
特点:
有序、有下标、元素可以重复。
方法:
- 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) //返回formIndex和toIndex之间的集合元素
List接口的使用01
//先创建集合对象
List list = new ArrayList<>();
//1.添加元素
list.add("apple");
list.add("banana");
list.add(0,"grape");
System.out.println(list.size());
System.out.println(list.toString());
//2.删除元素
list.remove("apple");
System.out.println(list.size());
System.out.println(list.toString());
//3.遍历元素
//(1)使用for遍历
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
//(2)使用增强for
for (Object object :list
) {
System.out.println(object);
}
//(3)迭代器
Iterator it = list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//(4)列表迭代器
ListIterator lit = list.listIterator();
while (lit.hasNext()){
System.out.println(lit.nextIndex()+":"+lit.next());
}
while (lit.hasPrevious()){
System.out.println(lit.previousIndex()+":"+lit.previous());
}
//4.判断
System.out.println(list.contains("grape"));
System.out.println(list.isEmpty());
//5.获取位置
System.out.println(list.get(0));
System.out.println(list.indexOf("banana"));
List接口的使用02
//创建集合
List list = new ArrayList();
//1.添加数字数据(自动装箱)
list.add(10);
list.add(200);
list.add(30);
list.add(40);
list.add(50);
list.add(10);
System.out.println(list.size());
System.out.println(list.toString());
//2.删除操作
// list.remove(new Integer(200));
// list.remove((Object) (20));
// list.remove(list.indexOf(20));
// System.out.println(list.size());
// System.out.println(list.toString());
//3.补充方法subList,返回子集合,含头不含尾
List subList = list.subList(0,2);
System.out.println(subList.toString());
List实现类
ArrayList:
-
数据结构实现,查询快、增删慢;
-
JDK1.2版本,运行效率快,线程不安全。
-
源码分析:
-
DEFAULT_CAPACITY = 10; 默认容量。
注意:如果集合中没有任何元素,容量为0;添加一个元素之后,容量为10;
每次扩容大小为原来的1.5倍。
-
elementData:存放元素的数组
-
size:实际元素个数
-
-
ArrayList的使用:
/* ArrayList的使用: 存储结构:数组,查找遍历速度快,增删慢 */ ArrayList arrayList = new ArrayList<>(); //1.添加元素 Student s1 = new Student("teemo",18); Student s2 = new Student("lily",10); Student s3 = new Student("dingding",12); Student s4 = new Student("papa",20); Student s5 = new Student("kaka",14); arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); arrayList.add(s4); arrayList.add(s5); System.out.println(arrayList.size()); System.out.println(arrayList.toString()); //2.删除元素 arrayList.remove(new Student("teemo",18));//equals(this==obj) System.out.println(arrayList.size()); System.out.println(arrayList.toString()); //3.遍历元素 //(1) for (2)forea //(3)迭代器 Iterator it = arrayList.iterator(); while (it.hasNext()){ Student student = (Student) it.next(); System.out.println(student.toString()); } //(4)列表迭代器 ListIterator lit = arrayList.listIterator(); while (lit.hasNext()){ Student student = (Student) lit.next(); System.out.println(student.toString()); } while (lit.hasPrevious()){ Student student = (Student) lit.previous(); System.out.println(student); } //4.判断 System.out.println(arrayList.contains(new Student("kaka",14))); System.out.println(arrayList.isEmpty()); //5.查找 System.out.println(arrayList.indexOf(new Student("kaka",14))); System.out.println(arrayList.get(1));
Vector:
-
数组结构实现,查询快、增删慢;
-
JDK1.0版本,运行效率慢,线程安全。
-
Vector的使用:
//创建集合 Vector vector = new Vector<>(); //1.添加元素 vector.add("mango"); vector.add("strawbery"); vector.add("grape"); vector.add("watermellon"); System.out.println(vector.size()); System.out.println(vector.toString()); //2.删除元素 // vector.remove(0); // vector.remove("watermellon"); // vector.clear(); // System.out.println(vector.size()); // System.out.println(vector.toString()); //3.遍历元素 //使用枚举器 Enumeration en = vector.elements(); while (en.hasMoreElements()){ String o = (String) en.nextElement(); System.out.println(o); } //4.判断 System.out.println(vector.contains("watermellon")); System.out.println(vector.isEmpty()); //5.Vector其他方法 System.out.println(vector.firstElement()); System.out.println(vector.lastElement()); System.out.println(vector.elementAt(0));
LinkedList:
- 链表结构实现,增删快,查询慢。
- LinkedList的使用
/*
LinkedList的使用
存储结构:双向链表
*/
//创建集合
LinkedList linkedList = new LinkedList();
//1.添加元素
Student s1 = new Student("teemo",18);
Student s2 = new Student("lily",10);
Student s3 = new Student("dingding",12);
Student s4 = new Student("papa",20);
Student s5 = new Student("kaka",14);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
linkedList.add(s4);
linkedList.add(s5);
System.out.println(linkedList.size());
System.out.println(linkedList.toString());
//2.删除元素
// linkedList.remove(s3);
// linkedList.remove(new Student("teemo",18));
// linkedList.clear();
// System.out.println(linkedList.size());
//3.遍历元素
//(1)for遍历
for (int i = 0; i < linkedList.size(); i++) {
System.out.println(linkedList.get(i));
}
//(2)增强for
for (Object o : linkedList) {
Student student = (Student)o;
System.out.println(student.toString());
}
//(3)使用迭代器(Iterator、ListIterator)
//Iterator
Iterator it = linkedList.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s.toString());
}
//ListIterator
ListIterator lit = linkedList.listIterator();
while (lit.hasNext()){
Student s = (Student) lit.next();
System.out.println(s.toString());
}
while (lit.hasPrevious()){
Student s = (Student) lit.previous();
System.out.println(s.toString());
}
//4.判断
System.out.println(linkedList.contains(new Student("lily",10)));
//5.获取元素
System.out.println(linkedList.indexOf(s2));
Set子接口
特点
无序、无下标、元素不可重复
方法
全部继承自Collection中的方法
Set接口的使用
/**
* 测试Set接口的使用
* 特点:(1)无序、没有下标 (2)不能重复
*/
public class SetDemo01 {
public static void main(String[] args) {
//创建集合
Set<String> set = new HashSet<>();
//1.添加数据
set.add("apple");
set.add("grape");
set.add("watermellon");
set.add("strawberry");
System.out.println(set.size());
System.out.println(set.toString());
//2.删除数据
set.remove("apple");
System.out.println(set.toString());
//3.遍历
//(1)增强for
for (String string: set
) {
System.out.println(string);
}
//(2)迭代器
Iterator<String> it = set.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//4.判断
System.out.println(set.contains("strawbery"));
System.out.println(set.isEmpty());
}
Set实现类
HashSet
- 基于HashCode实现,元素不重复;
- 当存入元素的哈希码相同时,会调用equals方法进行确认,如结果为true,则拒绝后者存入。
- HashSet的使用01:
/**
* HashSet集合的使用
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class HashSetDemo01 {
public static void main(String[] args) {
HashSet<String> hashSet = new HashSet<>();
//1.添加元素
hashSet.add("teemo");
hashSet.add("papa");
hashSet.add("dingdang");
hashSet.add("meow");
//hashSet.add("teemo");
System.out.println(hashSet.size());
System.out.println(hashSet.toString());
//2.删除数据
hashSet.remove("teemo");
System.out.println(hashSet.size());
System.out.println(hashSet.toString());
//3.遍历数据
//(1)增强for
for (String string: hashSet
) {
System.out.println(string);
}
//(2)使用迭代器
Iterator<String> it = hashSet.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//4.判断
System.out.println(hashSet.contains("papa"));
System.out.println(hashSet.isEmpty());
}
}
- HashSet的使用02
/**
* HashSet的使用
* 存储结构:哈希表(数组+链表+红黑树)
* 存储过程(重复的依据):
* (1)根据hashcode计算数据保存的位置,如果此位置为空,则直接保存,若不为空则执行第二步
* (2)再根据equals方法,如果equals方法为true,则认为数据重复,否则,形成链表
*/
public class HashSetDemo02 {
public static void main(String[] args) {
//创建集合
HashSet<Person> persons = new HashSet<>();
//1.添加数据
Person p1 = new Person("teemo", 18);
Person p2 = new Person("dami", 18);
Person p3 = new Person("dingding", 18);
Person p4 = new Person("papa", 18);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
persons.add(new Person("papa",18));//判断数据重复,需改写equals方法
System.out.println(persons.size());
System.out.println(persons.toString());
//2.删除操作
// persons.remove(p1);
// persons.remove(new Person("dami",18));
// System.out.println(persons.size());
// System.out.println(persons.toString());
//3.遍历
//(1)增强for
for (Person p:persons
) {
System.out.println(p.toString());
}
//(2)迭代器
Iterator<Person> it = persons.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//4.判断
System.out.println(persons.contains(p2));
System.out.println(persons.isEmpty());
}
}
//重写Person类中的hashCode和equals方法
@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);
}
TreeSet
- 基于排列顺序实现元素不重复;
- 实现了SortedSet接口,对集合元素自动排序;
- 元素对象的类型必须实现comparable接口,指定排序规则;
- 通过CompareTo方法确定是否为重复元素。
- TreeSet的使用01:
/**
* TreeSet的使用
* 存储结构:红黑树
*/
public class TreeSetDemo01 {
public static void main(String[] args) {
//创建集合
TreeSet<String> treeSet = new TreeSet<>();
//1.添加元素
treeSet.add("abc");
treeSet.add("apple");
treeSet.add("strawberry");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
//2.删除元素
// treeSet.remove("abc");
// System.out.println(treeSet.size());
//3.遍历
//(1)增强for
for (String s : treeSet) {
System.out.println(s);
}
//(2)迭代器
Iterator<String> it = treeSet.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//4.判断
System.out.println(treeSet.contains("abc"));
}
}
- TreeSet的使用02:
/**
* 使用TreeSet保存数据
* 存储结构:红黑树
* 要求:元素必须要实现Comparable接口,compareTo()方法返回值为0,认为是重复元素
*/
public class TreeSetDemo02{
public static void main(String[] args) {
//创建集合
TreeSet<Person> persons = new TreeSet<>();
//1.添加元素
Person p1 = new Person("teemo", 20);
Person p2 = new Person("dami", 18);
Person p3 = new Person("dingding", 18);
Person p4 = new Person("papa", 15);
Person p5 = new Person("papa", 10);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
persons.add(p5);
persons.add(new Person("papa",18));//判断数据重复,需改写equals方法
System.out.println(persons.size());
System.out.println(persons.toString());
//2.删除元素
persons.remove(new Person("teemo",20));
System.out.println(persons.size());
//3.遍历
//(1)增强for
for (Person person : persons) {
System.out.println(person.toString());
}
//(2)迭代器
Iterator<Person> it = persons.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//4.判断
System.out.println(persons.contains(new Person("teemo",20)));
}
}
//要求:Person元素必须要实现Comparable接口
public class Person implements Comparable<Person>{
private String name;
private int age;
public Person() {
}
public Person(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 "Person{" +
"name='" + name + '\'' +
", 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);
}
//先按姓名比,再按年龄比
@Override
public int compareTo(Person o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.getAge()-o.getAge();
return n1==0?n2:n1;
}
}
- TreeSet的使用(Comparator定制比较规则):
/**
* TreeSet集合的使用
* comparator:实现定制比较(比较器)
* comparable:可比较的
*/
public class TreeSetDemo03 {
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;
}
});
//1.添加数据
Person p1 = new Person("teemo", 18);
Person p2 = new Person("dami", 15);
Person p3 = new Person("dingding", 18);
Person p4 = new Person("papa", 20);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
persons.add(new Person("papa",18));//判断数据重复,需改写equals方法
System.out.println(persons.size());
System.out.println(persons.toString());
}
}
- TreeSet的使用(实现字符串按照长度进行排序):
/**
* 要求:使用TreeSet集合实现字符串按照长度进行排序
*定制比较规则:Comparator接口
*/
public class TreeSetDemo04 {
public static void main(String[] args) {
//创建集合并指定比较规则
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int n1 = o1.length()-o2.length();
int n2 = o1.compareTo(o2);
return n1==0?n2:n1;
}
});
//添加数据
treeSet.add("hello");
treeSet.add("apple");
treeSet.add("strawberry");
treeSet.add("abc");
System.out.println(treeSet.size());
System.out.println(treeSet.toString());
}
}
泛型
泛型概念:
Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。
常见形式:
泛型类
/*
泛型类
语法:类型<T>
T是类型占位符,表示一种引用类型,如果编写多个使用逗号隔开
*/
public class MyGeneric<T> {
//使用泛型T
//1.创建变量
T t;
//2.泛型作为方法的参数
public void show(T t){
System.out.println(t);
}
//3.泛型作为方法的返回值
public T getT(){
return t;
}
}
泛型接口
/*
泛型接口
语法:接口名<T>
注意:不能泛型静态常量
*/
public interface MyInterface<T> {
String name = "teemo";
T server(T t );
}
泛型方法
/*
泛型方法
语法:<T> 回值类型
*/
public class MyGenericMethod {
//泛型方法
//<T>声明泛型方法,T t作为泛型参数,T作为返回值类型
public <T> T show(T t){
System.out.println("generic method!!" + t);
return t;
}
}
语法
<T,…> T称为类型占位符,表示一种引用类型。
好处
- 提高代码的重用性;
- 防止类型转换异常,提高代码的安全性。
泛型集合
泛型集合概念
参数化类型、类型安全的集合,强制集合元素的类型必须一致。
特点
- 编译时即可检查,而非运行时抛出异常;
- 访问时,不必类型转换(拆箱);
- 不同泛型之间引用不能相互赋值,泛型不存在多态。
Map接口
特点:
存储一对数据(Key-Value),无序、无下标,键不可重复,值可重复。
方法:
-
V put(K key, V value) //将对象存入到集合中,关联键值。key重复则覆盖原值。
-
Object get(Object key) //根绝键获取对应的值。
-
Set //返回所有Key。
-
Collection values() //返回包含所有值的Collection集合。
-
Set<Map.Entry<K,V>> //键值匹配的Set集合。
Map接口的使用
/**
* Map接口的使用
* 特点:(1)存储键值对(2)键不能重复,值可以重复(3)无序
*/
public class MapDemo01 {
public static void main(String[] args) {
//创建集合
Map<String,String> map = new HashMap<>();
//1.添加元素
map.put("cn","zhongguo");
map.put("uk","yingguo");
map.put("usa","meiguo");
System.out.println(map.size());
System.out.println(map.toString());
//2.删除
// map.remove("usa");
// System.out.println(map.size());
//3.遍历
//(1)使用keySet();
// Set<String> keyset = map.keySet();
for (String s : map.keySet()) {
System.out.println(s+":"+map.get(s));
}
//(2)使用entrySet()方法
// Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "---" + entry.getValue());
}
//4.判断
System.out.println(map.containsKey("usa"));
System.out.println(map.containsValue("yingguo"));
}
}
Map实现类:
HashMap
- JDK1.2版本,线程不安全,运行效率快;允许用null作为key或value。
- HashMap集合的使用:
/**
* HashMap集合的使用
* 存储结构:哈希表(数组+链表+红黑树)
* 使用key的hashcode和equals作为重复判定
*/
public class HashMapDemo01 {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> students = new HashMap<>();
//1.添加元素
Student s1 = new Student("teemo",001);
Student s2 = new Student("lily",002);
Student s3 = new Student("papa",003);
students.put(s1,"beijing");
students.put(s2,"tianjin");
students.put(s3, "henan");
// students.put(s3, "nanjing");//重复的key会覆盖value
students.put(new Student("teemo",001),"hefei");
System.out.println(students.size());
System.out.println(students.toString());
//2.删除
// students.remove(s1);
// System.out.println(students.size());
//3.遍历
//(1)使用KeySet()
for (Student key : students.keySet()) {
System.out.println(key.toString()+"---"+students.get(key));
}
//(2)使用EntrySet()
for (Map.Entry<Student, String> entry : students.entrySet()) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
//4.判断
System.out.println(students.containsKey(new Student("teemo",001)));
System.out.println(students.containsValue("henan"));
}
}
Hashtable
- JDK1.0版本,线程安全,运行效率慢;不允许null作为key或value。
Properties
- Hashtable的子类,要求key和value都是String。通常用于配置文件的读取。
- Properties的使用:
/**
* Properties集合的使用
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
//1.创建集合
Properties properties = new Properties();
//2.添加数据
properties.setProperty("username","li");
properties.setProperty("age","20");
out.println(properties.toString());
//3.遍历
//3.1 keySet
//3.2 entrySet
//3.3 stringPropertyNames()
Set<String> propertyNames = properties.stringPropertyNames();
for (String pro : propertyNames) {
out.println(pro+":"+properties.getProperty(pro));
}
//4.和流有关的方法
//4.1 list方法
// PrintWriter pw = new PrintWriter("e:\\writer.txt");
// properties.list(pw);
// pw.close();
//4.2 store方法
// FileOutputStream fos = new FileOutputStream("e:\\store.properties");
// properties.store(fos,"注释");
// fos.close();
//4.3 load方法加载
Properties properties1 = new Properties();
FileInputStream fis = new FileInputStream("e:\\store.properties");
properties1.load(fis);
fis.close();
out.println(properties1.toString());
}
}
TreeMap
-
实现了SortedMap接口(是Map的子接口),可以对key自动排序。
-
TreeMap的使用:
/**
* TreeMap的使用
* 存储结构:红黑树
*/
public class TreeMapDemo01 {
public static void main(String[] args) {
//新建集合
TreeMap<Student, String> students = new TreeMap<Student, String>();
//1.添加元素
Student s1 = new Student("teemo",001);
Student s2 = new Student("lily",002);
Student s3 = new Student("papa",003);
students.put(s1,"beijing");
students.put(s2,"tianjin");
students.put(s3, "henan");
// students.put(s3, "nanjing");//重复的key会覆盖value
students.put(new Student("teemo",001), "hefei");
System.out.println(students.size());
System.out.println(students.toString());
//2.删除
students.remove(s2);
System.out.println(students.size());
//3.遍历
//(1)使用KeySet
for (Student key : students.keySet()) {
System.out.println(key.toString()+"---"+students.get(key));
}
//(2)使用EntrySet
for (Map.Entry<Student, String> entry : students.entrySet()) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
//4.判断
System.out.println(students.containsKey(new Student("teemo",001)));
System.out.println(students.containsValue("henan"));
}
}
Collections工具类
概念
集合工具类,定义了除了存取以外的集合常用方法。
方法
- public static void reverse(List<?> list) //反转集合中元素的顺序
- public static void shuffle(List<?> list) //随机重置集合元素的顺序
- public static void sort(List<?> list) //升序排序(元素类型必须实现Comparable接口)
Collections的使用
/**
* 演示Collections工具类的使用
*/
public class CollectionsDemo {
public static void main(String[] args) {
//创建集合
List<Integer> list = new ArrayList<>();
//添加元素
list.add(200);
list.add(2);
list.add(50);
list.add(3);
//sort排序
System.out.println("before sorted:"+list.toString());
Collections.sort(list);
System.out.println("after sorted: "+list.toString());
//binarySearch
int i = Collections.binarySearch(list, 50);
System.out.println(i);
//copy复制
List<Integer> dest = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
dest.add(0);
}
Collections.copy(dest,list);
System.out.println(dest.toString());
//reverse
Collections.reverse(list);
System.out.println(list);
//shuffle
Collections.shuffle(list);
System.out.println(list);
//补充:(1)list转成数组 (2)数组转成集合
//(1)list转成数组
Integer[] arr = list.toArray(new Integer[10]);
System.out.println(arr.length);
System.out.println(Arrays.toString(arr));
//(2)数组转成集合
String[] name = {"aa","bb","cc"};
//集合是一个受限集合,不能添加和删除
List<String> list1 = Arrays.asList(name);
//list1.add("mm");//会报错
System.out.println(list1);
//把基本类型数组转成集合时,需要修改为包装类型
Integer[] num = {10,2,500,30,4};
List<Integer> list2 = Arrays.asList(num);
System.out.println(list2);
}
}
集合总结
- 集合的概念:对象的容器,和数组类似,定义了对多个对象进行操作的常用方法。
- List集合:有序、有下标、元素可以重复。(ArrayList、LinkedList、Vector)
- Set集合:无序、无下标、元素不可重复。(HashSet、TreeSet)
- Map集合:存储一对数据,无序、无下标,键不可重复,值可重复。(HashMap、HashMap、TreeMap)
- Collections:集合工具类,定义了除了存取以外的集合常用方法。