集合框架之collection
一、集合框架
概念:对象的容器,实现了对对象常用的操作,类似数组功能。
集合和数组的区别:
1、数组长度固定,集合长度不固定
2、数组可以存储基本类型和引用类型,集合只能存储引用类型
位置:java.until.*
Collection体系集合
Collection父接口
1、特点:代表一组任意类型的对象,无序、无下标、不能重复
2、方法:
//看API帮助文档
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对象
int size()//返回此集合中的元素个数
Object[] toArray()//将此集合转换成数组
iterator()//集合遍历
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
/**
* Collection接口的使用
* (1)添加关系
* 2、删除关系
* 3、遍历关系
* 4、判断
*/
public class Main{
public static void main(String[] args) {
//创建集合
Collection collection = new ArrayList();
// *(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
System.out.println("------3.1使用增强for--------");
for(Object object:collection)
{
System.out.println(object);
}
//3.2使用迭代器(迭代器专门用来遍历集合的一种方式)
System.out.println("----------3.2使用迭代器----------");
//hasNext();有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
Iterator iterator=collection.iterator();
while(iterator.hasNext())
{
String s=(String)iterator.next();
//或者Object object = iterator.next();
System.out.println(s);
//不能使用collection的删除方法
//collection.remove(s);
//应用iterator.remove();
}
System.out.println("元素个数:"+collection.size());
// *4、判断
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* Collection的使用:保存学生信息
*
*/
public class Demo2 {
public static void main(String[] args) {
//新建Collection对象
Collection collection =new ArrayList();
Student s1=new Student("张三",18);
Student s2 = new Student("李四",20);
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.remove(new Student("张三",18));
//collection.clear();\
//3遍历
//3.1增强for
for(Object object:collection)
{
Student student=(Student)object;
System.out.println(student.toString());
}
//3.2iterator迭代器:hasNext(); next(); remove();
//迭代过程中不能使用collection的删除方法
Iterator it = collection.iterator();
while(it.hasNext())
{
Student student=(Student)it.next();
System.out.println(student.toString());
}
}
}
/**
* 学生类
* @author 鹏鹏
*
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
super();
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 + "]";
}
}
浙公网安备 33010602011771号