数组的不足
1)数组长度在定义时必须指定,而且一旦指定,不能更改;
2)保存的必须为同一类型的元素;
3)使用数组进行增加和删除。
集合
1)可以动态保存任意多个对象;
2)提供一系列方便操作对象的方法:add、remove、set、get;
3)使用集合添加、删除新元素的示意代码更简洁。
集合框架体系
单列集合
Iterable(接口) | ||||||
Collection(接口) | ||||||
List(接口)(下面为常用类) | Set(接口)(下面为常用类) | |||||
LinkedList(类) | ArrayList(类) | Vector(类) | ArrayLinkedList(类) | HashSet(类) | TreeSet(类) | LinkedHashSet(类) |
双列集合
Map(接口) | ||
Hashtable(类) | HashMap(类) | TreeMap(类) |
Properties(类) | LinkedHashMap(类) |
package com.pero.collection_;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @author Pero
* @version 1.0
*/
public class Collection_ {
public static void main(String[] args) {
//1.集合主要分两组(单列集合,双列集合)
//2.Collection接口有两个重要的子接口:List接口和set接口,其实现的子类都是单列集合
//3.Map接口实现的子类是双列集合,存放K-V
//单列集合
ArrayList arrayList = new ArrayList();
arrayList.add("jake");
arrayList.add("tom");
//双列集合
HashMap hashMap = new HashMap();
hashMap.put("number00","jake");
hashMap.put("number01","lucy");
}
}
Collection接口实现类的特点
1)Collection实现子类可以存放多个元素,每个元素都可以是Object;
2)有一些Collection实现子类可以存放重复元素,有一些不可以;
3)有一些Collection实现子类是有序的(List的实现子类),有一些不是有序的(set的实现子类);
4)Collection没有直接实现的子类,是通过其子接口(Set和List)的子类来实现的。
Collection常用方法
package com.pero.collection_;
import java.util.ArrayList;
import java.util.List;
/**
* @author Pero
* @version 1.0
*/
public class CollectionMethod {
public static void main(String[] args) {
List list = new ArrayList();
//add()方法,添加单个元素
list.add("jake");
list.add(10); //装箱:list.add(new Integer(10);
list.add(true); //装箱
//remove()方法,删除指定元素
list.remove("jake"); //删除指定元素名称的元素(返回boolean值)
list.remove(1); //删除指定索引位置的元素(返回被删除的对象)
//contains()方法,查找元素是否存在
list.contains(10); //返回boolean值
//size()方法,获取元素个数
list.size(); //返回元素个数
//isEmpty()方法,判断是否为空
list.isEmpty(); //返回boolean值
//clear()方法,清空集合元素
list.clear();
//addAll()方法,添加多个元素
List list1 = new ArrayList();
list1.add("lucy");
list1.add(68.36);
list.addAll(list1); //将list1集合中的元素全部添加到list集合中
//containsAll()方法,查找多个元素是否存在
list.contains(list1); //返回boolean值
//remove()方法,删除多个元素
list.removeAll(list1);
}
}
Collection接口遍历元素方式
1)方式一:使用Iterator(迭代器)
①Iterator对象称为迭代器,主要用于遍历Collection集合中的元素;
②所有实现Collection接口的集合类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象,即可以返回一个迭代器;
③Iterator的结构和运行图:
——【元素1】——【元素2】——【元素3】——【元素4】—.....—【最后一位元素】——
⬆(游标) 👆(游标指到最后一位)
Iterator iterator = collection.iterator(); //得到集合collection的一个迭代器
iterator.hasNext(); //判断是否还有下一个元素,如果没有返回false
Object object = iterator.next(); //游标下移;将下移以后位置上的元素返回
④Iterator仅用于遍历集合,Iterator本身并不存放集合对象;
2)方式二:使用增强for()循环
增强for()循环就是简化版的iterator,其底层使用的就是迭代器,只能用于遍历集合和数组;
基本语法:
for(元素类型 元素名 : 集合名或者数组名){
访问元素
}
3)方式三:使用普通for()循环
package com.pero.collection_;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* @author Pero
* @version 1.0
*/
public class CollectionIterator {
public static void main(String[] args) {
Collection collection = new ArrayList();
collection.add(new Book("三国演义","罗贯中",100));
collection.add(new Book("红楼梦","曹雪芹",120));
collection.add(new Book("水浒传","施耐庵",80));
//第一种方法:使用迭代器遍历集合
//1.先生成集合类对象的迭代器
Iterator iterator = collection.iterator();
//2.使用while循环遍历
while (iterator.hasNext()){ //hasNext()方法,判断是否还有元素
//返回下一个元素,类型是Object
Object object = iterator.next(); //编译类型是Object,运行类型是存储元素的运行类型
System.out.println(object);
}
//快速生成迭代器的快捷键itit
//3.当退出while循环后,迭代器iterator指向的是最后一个元素
//如果想重新遍历集合,需要重置迭代器,否则会抛出NoSuchElementException异常
//iterator = collection.iterator(); //重置迭代器,接下来可以第二次遍历
//第二种方法:使用增强for()遍历集合,底层依然是迭代器
for(Object book:collection){
System.out.println(book);
}
//第三种方法:使用普通for()遍历集合
for (int i = 0; i < collection.size(); i++) {
List list = (List) collection;
System.out.println(list.get(i));
}
}
}
class Book{
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}