什么是集合
概念:对象的容器,定义了对多个对象进行操作的操作方法。可实现数组的功能。
和数组的区别:数组长度固定,集合长度不固定。
数组可以存储基本类型和引用类型,集合只能存储引用类型
Collection体系集合
1.直接添加元素
package com.Colletion;
/**
* collection的使用(1)
*/
import com.oop.demo05.A;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01 {
/**
Collection接口的使用
1.添加元素
2.删除元素
3.遍历元素
4.判断
*/
public static void main(String[] args) {
Collection collection = new ArrayList();
// 1.添加元素
collection.add("苹果");
collection.add("西瓜");
collection.add("香蕉");
collection.add("梨");
collection.add("榴莲");
System.out.println("元素个数:"+collection.size());
System.out.println(collection);
// 2.删除元素
collection.remove("香蕉");
// collection.clear(); 清空
System.out.println("删除之后:"+collection);
System.out.println("======使用增强for遍历=======");
//3.遍历元素
//3.1增强for (for是不行的 因为没有下标)
for (Object object:collection
) {
System.out.println(object);
}
//3.2使用迭代器(迭代器是专门用来遍历集合的一种方式)
//hasNext();有没有下一个元素
//next();获取下一个元素
//remove();删除当前元素
System.out.println("==========使用迭代器遍历==========");
Iterator it = collection.iterator();
while (it.hasNext()){
String s = (String)it.next();
System.out.println(s);
it.remove();
// 可以使用it.remove(); 进行移除元素
// collection.remove(); 不能用collection其他方法 会报并发修改异常
}
System.out.println("集合内元素个数:"+collection.size());
// 4.判断
System.out.println(collection.contains("西瓜"));
System.out.println(collection.isEmpty());
}
}
2.把类的对象作为集合元素
package com.Colletion;
/*
collection的使用(2)
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo02 {
public static void main(String[] args) {
//新建collection对象
Collection collection = new ArrayList();
Student s1 = new Student("张三",20);
Student s2 = new Student("老八",88);
Student s3 = new Student("李白",999);
//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("老八",88)); 这个是并没有移除的 因为new了一个 生成了新的Student 只不过属性和s2一样
System.out.println(collection.size());
System.out.println(collection.toString());
//3.(1)遍历 增强for
System.out.println("=======增强for遍历============");
for (Object object:collection
) {
Student s = (Student) object;
System.out.println(s.toString());
}
//3.(2) 迭代器遍历 迭代过程中不能用collection的删除方法
System.out.println("=========使用迭代器遍历===========");
Iterator it = collection.iterator();
for (;it.hasNext();){
Student s = (Student) it.next();
System.out.println(s.toString());
}
//4.判断
System.out.println(collection.contains(s2));
}
}
List集合(Collction的子接口)
package com.Colletion;
import com.oop.demo05.A;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* 子接口List的使用
* 有序 有下标 可重复
*/
public class Demo03 {
public static void main(String[] args) {
List list = new ArrayList();
//添加元素
list.add("臭豆腐");
list.add("腐乳");
list.add("奥利给");
System.out.println("集合内元素个数:"+list.size());
System.out.println(list.toString());
//2.删除元素
// list.remove("腐乳");
// System.out.println("删除之后:"+ list.size());
// System.out.println(list.toString());
//3.0遍历 for循环(因为list是有下标的)
System.out.println("=========for循环遍历,list有下标============");
for (int i=0;i<list.size();i++){
System.out.println(list.get(i)); //和数组不一样,list用的是list.get(index)来获取元素
}
//3.1遍历 增强for
System.out.println("=========增强for遍历============");
for (Object object:list
) {
System.out.println(object);
}
//3.2遍历 迭代器
System.out.println("=========迭代器============");
Iterator it = list.iterator();
while (it.hasNext()){
String s = (String) it.next();
System.out.println(s);
}
//3.4遍历 列表迭代器(new!!!),和Iterator的区别,listiterator可以向前或者向后遍历,添加,删除,修改元素
ListIterator listIterator= list.listIterator();
System.out.println("========3.4使用列表迭代器从前往后======");
while (listIterator.hasNext()){
System.out.println(listIterator.nextIndex()+":"+listIterator.next());
}
System.out.println("========3.5使用列表迭代器从后往前======");
while (listIterator.hasPrevious()){
System.out.println(listIterator.previousIndex()+":"+listIterator.previous());
}
System.out.println(list.contains("臭豆腐"));
System.out.println(list.isEmpty());
//5.获取位置
System.out.println(list.indexOf("奥利给"));
}
}
关于数字作为元素的list
package com.Colletion;
import java.util.ArrayList;
import java.util.List;
/**
* List的使用
*/
public class Demo04 {
public static void main(String[] args) {
List list = new ArrayList();
//添加数字数据(自动装箱)
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
list.add(70);
System.out.println("元素个数:"+list.size());
System.out.println(list.toString());
//2 删除
// list.remove(0);
// list.remove(new Integer(20));
list.remove((Object)20);
System.out.println(list.toString());
List subL=list.subList(1,4);
System.out.println(subL);
}
}
Arraylist (Student类中 equals的重写)
new Student比较Student中原有对象,如果属性相等,则返回ture,删除new Student 就删除原有对象
public boolean equals(Object o) {
if(this==o){
return true;
}
if(o==null){
return false;
}
if(o instanceof Student){
Student s = (Student)o;
//比较属性
if(this.name.equals(s.name)&&this.age==s.age){
return true;
}
}
return false;
}
ArrayList arrayList = new ArrayList();
Student s1 = new Student("刘德华",20);
Student s2 = new Student("张学友",22);
Student s3 = new Student("郭富城",21);
s3.toString();
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println(arrayList.toString());
// arrayList.remove(new Student("张学友",22));
// //这里重写了Student的equals方法,判断如果这个new Student和原有的this的属性一致,那么这里删除的就是已创建的Student对象
//
// System.out.println(arrayList.toString());
Arraylist源码分析
默认容量DEFAULT_CAPACITY=10;
如果没有向集合中添加任何元素,容量为0
存放元素的数组elementData
实际的元素个数 size
add()方法
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
泛型
Java泛型只能是引用类型
泛型类
public class MyGeneric<T> {
//使用泛型T
//1.创建变量
T t;
//2.泛型作为 方法的参数
public void show(T t){
System.out.println(t);
}
//3.泛型作为 方法的返回值
public T getT(){
return t;
}
}
Test
MyGeneric<String> myGeneric = new MyGeneric<String>();
//泛型变量
myGeneric.t="嗨害嗨";
//泛型作为方法参数
myGeneric.show("来了嗷"); //show(T t)
//泛型作为方法的返回值
String s = myGeneric.getT(); //return t
System.out.println(s);
MyGeneric<Integer> myGeneric1 = new MyGeneric<Integer>();
myGeneric1.t=100;
myGeneric1.show(200);
int i = myGeneric1.getT();
System.out.println(i);
泛型接口
接口
public interface MyInterface<T>{
String name="张三";
T server(T t);
}
接口的实现1
public class MyInterfaceImpl implements MyInterface<String>{
@Override
public String server(String s) {
System.out.println(s);
return s;
}
}
接口的实现2
public class MyInterfaceImpl2<T> implements MyInterface<T> {
@Override
public T server(T t) {
System.out.println(t);
return t;
}
}
泛型接口测试
MyInterfaceImpl impl = new MyInterfaceImpl();
impl.server("嗨害嗨");
//泛型接口的使用(2)
MyInterfaceImpl2<Integer> impl2 = new MyInterfaceImpl2<>();
impl2.server(798);
泛型方法
public class MyGenericMethod {
//泛型方法
public <T> T show(T t){
System.out.println("泛型方法:"+t);
return t;
}
}
//泛型方法
MyGenericMethod mgm = new MyGenericMethod();
String s1 = "泛型方法之老八表演吃粑粑";
mgm.show(s1);
mgm.show(666);
mgm.show(3.1415926);