集合框架
ArrayList源码分析
默认容量大小 DEFAULT_CAPACITY = 10;
注意:如果没有向集合中添加任何元素,容量0,添加一个元素后,容量10
扩容:每次扩容原来的1.5倍
存放元素数组 elementData
实际元素个数 size
add() 添加数组
Increments modCount 增加数组
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
Vector:
package J;
import java.util.Enumeration;
import java.util.Vector;
public class Demo06 {
public static void main(String[] args) {
//创建集合
Vector vector = new Vector<>();
//1.添加元素
vector.add("草莓");
vector.add("香蕉");
vector.add("西瓜");
System.out.println("元素个数:"+vector.size());
//2.删除
vector.remove(0);
vector.remove("西瓜");
vector.clear();
//3.遍历
//使用枚举器
Enumeration en = vector.elements();
while(en.hasMoreElements()){
String o = (String)en.nextElement();
System.out.println(o);
}
//判断
System.out.println(vector.contains("西瓜"));
System.out.println(vector.isEmpty());
//5.其他方法
//firstElement,lastElement,ElementAt()
}
}
LinkedLiset使用
存储结构:双向链表
链表结构实现,增减快,查询慢
package Q;
//LinkedList的使用
import J.Student;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
public class Demo02 {
public static void main(String[] args) {
//创建集合
LinkedList linkedList = new LinkedList<>();
//1.添加元素
Student s1 = new Student("刘德华",20);
Student s2 = new Student("郭富城",22);
Student s3 = new Student("梁朝伟",18);
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
//2.删除
// linkedList.remove(s1);
// linkedList.remove(new Student("刘德华",20));//重写equals方法
// System.out.println("删除之后:"+linkedList.size());
// linkedList.clear();
System.out.println("=============================");
//3.遍历
//3.1for
for (int i = 0;i<linkedList.size();i++){
System.out.println(linkedList.get(i));
}
System.out.println("=============================");
//3.2增强for
for (Object object : linkedList){
Student s = (Student)object;
System.out.println(s.toString());
}
System.out.println("=============================");
//3.3使用迭代器
Iterator it = linkedList.iterator();
while(it.hasNext()){
Student s = (Student)it.next();
System.out.println(s.toString());
}
System.out.println("=============================");
//3.4列表迭代器
ListIterator lit = linkedList.listIterator();
while(lit.hasNext()){
Student s = (Student)lit.next();
System.out.println(s.toString());
}
//4.判断
System.out.println(linkedList.contains(s1));
System.out.println(linkedList.isEmpty());
//获取
System.out.println(linkedList.indexOf(s1));
}
}
泛型概述
Java泛型是JDK1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。
常见形式有泛型类,泛型接口,泛型方法。
语法:
<T,…> T成为类型占位符,表示一种引用类型。
好处:
(1)提高代码的重用性
(2)防止类型转换异常,提高代码的安全性
/**泛型类
* 语法:类名<T>
* T是类型占位符,表示一种引用类型,如果编写多个用逗号隔开
*/
public class MyGeneric<T> {
//使用泛型T
//1.创建变量
T t;
//2.泛型作为方法的参数
public void show(T t){
//T t1 = new T(); 不能实例化,因为不能确定传递过来的参数
System.out.println(t);
}
//3.使用泛型作为方法的返回值
public T getT(){
return t;
}
}
public class TestGeneric {
public static void main(String[] args) {
//使用泛型类创建对象
//注意:1.泛型只能使用引用类型。2.不同泛型对象之间不能相互赋值
MyGeneric<String> myGeneric = new MyGeneric<>();
myGeneric.t = "hello";
myGeneric.show("大家好");
String string = myGeneric.getT();
MyGeneric<Integer>myGeneric2 = new MyGeneric<Integer>();
myGeneric2.t = 100 ;
myGeneric2.show(200);
Integer integer = myGeneric2.getT();
}
}
泛型接口
/**
* 泛型接口
* 语法:接口名<T>
*/
public interface MyInterface<T> {
String name = "张三";
T server(T t);
}
//定义接口为String类
package Q;
public class MyInterfaceImpl implements MyInterface<String>{
@Override
public String server(String t) {
System.out.println(t);
return t;
}
}
//没有定义类型
//将类也改为泛型,由类确定,再传递给接口
public class MyInterfaceIpml2<T> implements MyInterface<T>{
@Override
public T server(T t) {
System.out.println(t);
return null;
}
}
public class TestGeneric {
public static void main(String[] args) {
MyInterfaceImpl impl = new MyInterfaceImpl();
impl.server("XXXX");
MyInterfaceIpml2<Integer> impl2 = new MyInterfaceIpml2<>();
impl2.server(1000);
}
}
泛型方法
package Q;
/**
* 泛型方法
* 语法:<T> 返回值类型
*/
public class MyGenericMethod {
//泛型方法
public <T> T show(T t){
System.out.println("泛型方法");
return t;
}
}
//泛型方法的使用
MyGenericMethod myGenericMethod = new MyGenericMethod();
myGenericMethod.show("中国牛逼");
myGenericMethod.show(3.14);
myGenericMethod.show(200);//类型由传递的数据决定
泛型集合
概念:参数化类型,类型安全的集合,强制集合元素的类型必须一致
特点:
编译时即可检查,而非运行时抛出异常。
访问时,不必类型转换(拆箱)。
不同反省之间引用不能相互赋值,泛型不存在多态。
强制集合元素的类型必须一致
for (Object object : arrayList){
String str = (String)object;
System.out.println(str);
}
报错
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at Q.Demo03.main(Demo03.java:17)
定义了数据类型
public class Demo03 {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("XXX");
arrayList.add("yyy");
arrayList.add(10);//报错
arrayList.add(20);//报错
ArrayList<Student> arrayList2 = new ArrayList<Student>();
Student s1 = new Student("刘德华",20);
Student s2 = new Student("郭富城",22);
Student s3 = new Student("梁朝伟",18);
arrayList2.add(s1);
arrayList2.add(s2);
arrayList2.add(s3);
Iterator<Student> it = arrayList2.iterator();
while(it.hasNext()){
Student s = it.next();
System.out.println(s.toString());