・集合类的特点
提供一种存储空间可变的存储模型,存储的瓣容量可以随时发生改变
Collection集合概述
Collection集合基本使用
public class col1ectionDemoOl ( public static vold main(string[] args) {
//创建co】】ecti on集合的对象
Col1ection<Stri ng> c = new ArrayList<Stri ng>();
//添加元素:boolean add(E e)
c.add(”hello”);
c.add("worid");
c.add("java");
//输出集合对象
System.out.pri ntln(c);
}
}
方法名 | 说明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中移除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否 g 旨定的元素 |
boolean isEmptyO | 判断集合是否为空 |
int size() | 集合的长度 |
public class Demo {
public static void main(String[] args) {
/*
Collection接口 常用功能:
- public boolean add(E e): 把给定的对象添加到当前集合中 。
- public void clear() :清空集合中所有的元素。
- public int size(): 返回集合中元素的个数。
- public boolean remove(E e): 把给定的对象在当前集合中删除。
- public boolean contains(Object obj): 判断当前集合中是否包含给定的对象。
- public boolean isEmpty(): 判断当前集合是否为空。
- public Object[] toArray(): 把集合中的元素,存储到数组中
*/
// 创建一个Collection集合对象,指定集合中元素的类型为String
Collection<String> coll = new ArrayList<>();
// 往集合中添加元素: public boolean add(E e)
coll.add("张三");
coll.add("李四");
coll.add("王二");
coll.add("赵六");
System.out.println("添加元素后的集合:"+coll);
boolean flag1 = coll.remove("张三");
System.out.println("flag1:"+flag1);// flag1:true
System.out.println("删除张三元素后的集合:"+coll);
boolean flag2 = coll.contains("张三");
System.out.println("张三这个元素是否存在:"+flag2);// false
boolean flag3 = coll.contains("赵四");
System.out.println("赵四这个元素是否存在:"+flag3);// true
// 判断集合中是否还有元素:public boolean isEmpty()
boolean flag4 = coll.isEmpty();
System.out.println("集合中是否还有元素:"+flag4);// false
/*coll.clear();
boolean flag5 = coll.isEmpty();
System.out.println("集合中是否还有元素:"+flag5);// true*/
// 把集合转换为数组:public Object[] toArray() 本质是:把该集合中的元素存储到一个数组中
Object[] arr = coll.toArray();
System.out.println("数组:"+ Arrays.toString(arr));
}
}
迭代器的介绍
迭代器,集合的专用
Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator方法得到
迭代器是通过集合的iterator。方法得到的,所以我们说它是依赖于集合而存在的
Collection集合的遍历
public static void main(String[] args) throws Exception{
Collection<String> coll = new ArrayList<>();
coll.add("张三");
coll.add("李四");
coll.add("王二");
coll.add("赵六");
Iterator<String> it = coll.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
//coll.add("章子怡");// 报异常
//coll.remove(s);// 报异常
it.remove();
}
集合不使用泛型的时候,存的时候什么类型都能存。但是取的时候就懵逼了。取出来啥也不是。
public class Demo_01不使用泛型 {
public static void main(String[] args) {
//创建集合对象
Collection c = new ArrayList();
//添加元素
c.add("张三");
c.add(123);
c.add(3.14);
//判断元素的长度是否大于4
//获取迭代器
Iterator it = c.iterator();
//ClassCastException类型转换异常
while (it.hasNext()) {
String s = (String)it.next();
int len = s.length();
System.out.println("打印字符串的长度" + len);
}
}
}
public class Demo02_使用泛型 {
public static void main(String[] args) {
//创建集合
Collection<String> list = new ArrayList<>();
//添加元素
list.add("张三");
//list.add(123); //在存储的时候就对类型作出了编译判断
list.add("李四");
//取出来的也都是字符串
}
}
修饰符 class 类名<代表泛型的变量> { }
代表泛型的变量: 可以是任意字母 例如: T,E...
class ArrayList<E>{
public boolean add(E e){ }
public E get(int index){ }
....
}
例如,ArrayList
此时,变量E的值就是String类型,那么我们的类型就可以理解为:
class ArrayList<String>{
public boolean add(String e){ }
public String get(int index){ }
...
}
定义格式:
修饰符 <代表泛型的变量> 返回值类型 方法名(参数){ }
例如:
public class MyGenericMethod {
public <MVP> void show(MVP mvp) {
System.out.println(mvp.getClass());
}
public <MVP> MVP show2(MVP mvp) {
return mvp;
}
}
public class GenericMethodDemo {
public static void main(String[] args) {
// 创建对象
MyGenericMethod mm = new MyGenericMethod();
// 演示看方法提示
mm.show("aaa");
mm.show(123);
mm.show(12.45);
}
}
定义格式:
修饰符 interface接口名<代表泛型的变量> { }
例如:
public interface MyGenericInterface<E>{
public abstract void add(E e);
public abstract E getE();
}
1、定义实现类时确定泛型的类型
public class MyImp1 implements MyGenericInterface<String> {
@Override
public void add(String e) {
// 省略...
}
@Override
public String getE() {
return null;
}
}
此时,泛型E的值就是String类型。
2、始终不确定泛型的类型,直到创建对象时,确定泛型的类型
public class MyImp2<E> implements MyGenericInterface<E> {
@Override
public void add(E e) {
// 省略...
}
@Override
public E getE() {
return null;
}
}
//确定泛型:
/*
* 使用
*/
public class GenericInterface {
public static void main(String[] args) {
MyImp2<String> my = new MyImp2<String>();
my.add("aa");
}
}
泛型的通配符:不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。
此时只能接受数据,不能往该集合中存储数据。
例如:
public static void main(String[] args) {
Collection<Intger> list1 = new ArrayList<Integer>();
getElement(list1);
Collection<String> list2 = new ArrayList<String>();
getElement(list2);
}
public static void getElement(Collection<?> coll){}
//?代表可以接收任意类型
//泛型不存在继承关系 Collection
之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限和下限。
泛型的上限:
类型名称 extends 类 > 对象名称
只能接收该类型及其子类
泛型的下限:
类型名称 super 类 > 对象名称
只能接收该类型及其父类型
比如:现已知Object类,String 类,Number类,Integer类,其中Number是Integer的父类
public static void main(String[] args) {
Collection<Integer> list1 = new ArrayList<Integer>();
Collection<String> list2 = new ArrayList<String>();
Collection<Number> list3 = new ArrayList<Number>();
Collection<Object> list4 = new ArrayList<Object>();
getElement1(list1);
getElement1(list2);//报错
getElement1(list3);
getElement1(list4);//报错
getElement2(list1);//报错
getElement2(list2);//报错
getElement2(list3);
getElement2(list4);
}
// 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
public static void getElement1(Collection<? extends Number> coll){}
// 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
public static void getElement2(Collection<? super Number> coll){}
方法名 | 描述 |
---|---|
void add(int index,E element) | 在此集合中的指定位置插/M旨定的元素 |
E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
E set(int index,E element) | 修改J旨定索引处的元素,返回被修改的元素 |
E get(int index) | 返回指定索引处的元素 |
• 解决的方案:
用for循环遍历,然后用集合对象做对应的操作即可
public class ListDemo (
public static vold main(string[] args) (
//创建集合对象
List<string> list = new ArrayList<string>();
//添加元素
list.add(”hello”);
list.add("world");
list.add("java");
//遍历集合,得到每f 元素,看有没有”world”这个元素,如果有,我就添加一
//个” javaee”元素,请写代码实现
// iterator it = list.iterator();
// whi 1 e (it.hasNextO) {
// Stri ng s = i t.next();
// if(s.equals("world")) {
// 11 st.add("javaee");
// }
// }
for(int 1=0; i<list.size(); i++) {
Stri ng s = list.get(i)
if(s.equals("world")) (
list.add("javaee");
}
}
//输出集合对象
System.out.pri ntln(list);
}
}
for(元素数据类型变量名:数组/集合对象名){
循环体;
}
public class ForDemo {
public static vold main(string[] args) (
int[] arr = {1,2,3,4,5};
for(i nt i : arr) {
System.out.pri ntln(i);
}
System, out. println(" ");
string[] strArray = {"hello","world","java"};
for(string s : strArray) {
System.out.pri ntln(s);
}
System, out. println(" ");
List<String> list = new ArrayList<String>();
list.add(”hello”);
list.add("worid");
list.add("java");
for(string s : list) {
System.out.pri ntln(s);
}
System, out. println(" ");
//内咅B原理是—iterator迭代器
/*
for(string s : list) {
if(s.equals("world")) {
list.add("javaee"); //concurrentModificationException
}
}
}
}
方法名 | 说明 |
---|---|
public ArrayLi st() | 创建一个空的集合对象 |
方法名 | 说明 |
---|---|
public boolean remove(Object o) | 删除指定的元素,返回删除是否成功 |
public E remove(int index) | 删除指定索引处的元素,返回被删除的元素 |
public E set(int index,E element) | 修改指定索引处的元素,返回被修改的元素 |
public E get(int index) | 返回指定索引处的元素 |
public int size() | 返回集合中的元素的个数 |
public boolean add(E e) | 将旨定的元素趙口到此集合的末尾 |
public void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
方法名 | 说明 |
---|---|
public void addFirst(E e) | 在该列券头插涌定的元素 |
public void addLast(E e) | 樹旨定的元素斷到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() | 从此列表中删除并返回最后一个元素 |