ArrayList
特点
可重复 有序的
和数组的区别
数组是固定大小
List是可以随时扩充的 按照10位扩充
代码
main
package JAVA.Collection;
import java.util.ArrayList;
public class List_test {
public static void main(String[] args) {
//使用几个的步骤
//1.创建集合对象
ArrayList list = new ArrayList();
//2.创建对象元素
Student s1 = new Student("林宏程", 11);
Student s2 = new Student("林宏", 21);
Student s3 = new Student("林", 31);
Student s4 = new Student("l", 41);
//3.将元素添加到List中
list.add(s1);//会直接添加到list中 返回的是一个boolean类型 告诉你有没有加入成功 这里的类型是泛型 当前可以看成Object类
list.add(s2);
list.add(s3);
list.add(s4);
//直接打印集合
System.out.println(list);//[Student{name='林宏程', age=11}, Student{name='林宏', age=21}, Student{name='林', age=31}, Student{name='l', age=41}] 表明这是有序的
//获取索引为2的对象
System.out.println(list.get(2));//Student{name='林', age=31}
//获取集合中的元素个数
System.out.println(list.size());//4
//4.遍历集合
for (int i = 0; i <list.size() ; i++) {
//i表示每个元素的索引
//获取第i个元素
System.out.println(list.get(i));
}
/*Student{name='林宏程', age=11}
Student{name='林宏', age=21}
Student{name='林', age=31}
Student{name='l', age=41}
*/
}
}
Student
package JAVA.Collection.List_test;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
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 +
'}';
}
}
增强for循环遍历
代码
main
package JAVA.Collection.List_test;
import java.util.ArrayList;
public class List_test {
public static void main(String[] args) {
//使用几个的步骤
//1.创建集合对象
ArrayList list = new ArrayList();
//2.创建对象元素
Student s1 = new Student("林宏程", 11);
Student s2 = new Student("林宏", 21);
Student s3 = new Student("林", 31);
Student s4 = new Student("l", 41);
//3.将元素添加到List中
list.add(s1);//会直接添加到list中 返回的是一个boolean类型 告诉你有没有加入成功 这里的类型是泛型 当前可以看成Object类
list.add(s2);
list.add(s3);
list.add(s4);
//4.遍历集合
for (Object o : list) {
System.out.println(o);
}//增强for循环底层依赖的是迭代器 增强for就是迭代器的简写
/*Student{name='林宏程', age=11}
Student{name='林宏', age=21}
Student{name='林', age=31}
Student{name='l', age=41}
*/
}
}
Student
代码如上
迭代器
对过程的重复 称为迭代
迭代器是遍历集合的通用方式 可以同时添加和删除
常用方法
Iterator和ListIterator的区别
package JAVA.Collection.List_test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class List_test {
public static void main(String[] args) {
//使用几个的步骤
//1.创建集合对象
ArrayList list = new ArrayList();
//2.创建对象元素
Student s1 = new Student("林宏程", 11);
Student s2 = new Student("林宏", 21);
Student s3 = new Student("林", 31);
Student s4 = new Student("l", 41);
//3.将元素添加到List中
list.add(s1);//会直接添加到list中 返回的是一个boolean类型 告诉你有没有加入成功 这里的类型是泛型 当前可以看成Object类
list.add(s2);
list.add(s3);
list.add(s4);
//4.3迭代器遍历
//需求 判断集合中如果有名字林宏程 后再加个新的对象
//1根据集合对象获取其对象的迭代器对象
ListIterator t = list.listIterator();//这个普通迭代器只能遍历 不能添加 只能删除
ListIterator it = list.listIterator();
//2判断迭代器有没有元素
while (it.hasNext()){//判断还有没有元素
Student s = (Student)it.next(); //3如果有就获取元素 这里类型是Student 所以接收的也是Student
if("林宏程".equals(s.getName())){
Student ns = new Student("新的对象",11);
//list.add(ns);//这样写是不行的 会发生 ConcurrentModificationException 必须调用 列表迭代器 的方法来实现
it.add(ns);
}
System.out.println(s);
}
System.out.println(list);//因为在迭代器中 他遍历的是集合list list在迭代之前就已经固定是4个对象s1 s2 s3 s4 他迭代的只能是固定的那4个
//如果你要添加进去只能等遍历完成后 才加入 所以过程是 迭代完成 迭代器加入到list中
}
}
泛型
代码
package JAVA.Collection.List_test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;
public class List_test {
public static void main(String[] args) {
//使用几个的步骤
//1.创建集合对象
ArrayList list = new ArrayList();
ArrayList<Student> ls = new ArrayList<Student>();//泛型
//2.创建对象元素
Student s1 = new Student("林宏程", 11);
Student s2 = new Student("林宏", 21);
Student s3 = new Student("林", 31);
Student s4 = new Student("l", 41);
//3.将元素添加到List中
ls .add(s1);
ls .add(s2);
ls .add(s3);
ls .add(s4);
// ls.add("aa");//这里因为使用了泛型 当你加入一个不对的对象 他就会报错
//直接打印集合
System.out.println(list);//[Student{name='林宏程', age=11}, Student{name='林宏', age=21}, Student{name='林', age=31}, Student{name='l', age=41}] 表明这是有序的
//获取索引为2的对象
System.out.println(list.get(2));//Student{name='林', age=31}
//获取集合中的元素个数
System.out.println(list.size());//4
//4.遍历集合
ListIterator<Student> sli = ls.listIterator();
while (sli.hasNext()){
System.out.println(sli.next());
}//泛型的迭代遍历是一样的 而且这里不再需要类型的转换
总结
-
泛型按只能和集合类相结合使用
-
泛型是JDK5新特性 但是JDK7可以不用写后面的类型 ArrayList ls = new ArrayList<这里可以不写了>();
Collections工具类
代码
package JAVA.JAVASE.Collection.List_test;
import java.util.ArrayList;
import java.util.Collections;
public class Collections_test {
public static void main(String[] args) {
//创建集合对象
ArrayList<Integer> list = new ArrayList<>();
list.add(3);
list.add(1);
list.add(3);
list.add(6);
System.out.println(list);//[3, 1, 3, 6]
//获取最大元素
System.out.println(Collections.max(list));//6
//对集合进行升序
Collections.sort(list);//本身不返回值 是void 对list直接改动
System.out.println(list);//[1, 3, 3, 6]
//进行反转 因为之前升序 如果反转之后就是降序 可以搭配使用实现降序
Collections.reverse(list);
System.out.println(list);//[6, 3, 3, 1]
//随机置换
Collections.shuffle(list);
System.out.println(list);//随机排列
}
}