List集合三个字实现类特点:
ArrayList
底层数据结构:可变数组的实现。
查询快,增删慢
从线程安全方面考虑:此实现不是同步的,线程不安全,执行效率高!
Vector
底层数据结构:对象数组
查询快,增删慢
线程安全角度考虑:Vector 是同步的,线程安全,执行效率低
LinkedList(特有功能:模拟栈结构的特点)
底层数据结构:
链接列表实现
查询慢,增删快
线程安全角度考虑: 此实现不是同步的,线程不安全,执行效率高
如果不知道使用什么集合的时候,默认都用ArrayList完成!
ArrayList
1、 使用ArrayList集合描述一个班级的学生
ArrayList
同时有存在很多班级,每一个班级也可以使用ArrayList集合表示
ArrayList<ArrayList>
import java.util.ArrayList;
/*
* 集合的嵌套遍历:
* 单列集合:
* 明确大的集合的存储的数据类型,在明确每一个小集合中存储的数据类型
* 增强for(数据类型 变量名 :集合对象){
* ...
* }
*
* */
public class ArrayListTest {
public static void main(String[] args) {
//创建一个大的集合对象:描述的每一个班,把每一个班看成ArrayList
ArrayList<ArrayList<Student>> array =
new ArrayList<ArrayList<Student>>() ;
//第一个小集合ArrayList<Student>
//创建第一个班级的学生
ArrayList<Student> firstArray = new ArrayList<Student>() ;
//创建学生对象
Student s1 = new Student("刘备",38) ;
Student s2 = new Student("张飞",30) ;
Student s3 = new Student("关羽",35) ;
//添加到当前集合中
firstArray.add(s1) ;
firstArray.add(s2) ;
firstArray.add(s3) ;
//把firstArray添加到大集合中
array.add(firstArray) ;
//创建第一个班级的学生
ArrayList<Student> secondArray = new ArrayList<Student>() ;
//创建学生对象
Student s4 = new Student("曹操",38) ;
Student s5 = new Student("马超",30) ;
Student s6 = new Student("夏侯惇",35) ;
//添加到当前集合中
secondArray.add(s4) ;
secondArray.add(s5) ;
secondArray.add(s6) ;
//添加到大集合中
array.add(secondArray) ;
ArrayList<Student> thirdArray = new ArrayList<Student>() ;
//创建学生对象
Student s7 = new Student("宋江",38) ;
Student s8 = new Student("吴庸",30) ;
Student s9 = new Student("武松",35) ;
//添加到当前集合中
thirdArray.add(s7) ;
thirdArray.add(s8) ;
thirdArray.add(s9) ;
//添加到大集合中
array.add(thirdArray) ;
//增强for循环遍历
for(ArrayList<Student> big : array ) {
for(Student s :big) {
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
}
//student类
public class Student {
private String name ;
private int age ;
public Student() {
super();
}
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 + "]";
}
}
2、获取10个1-20之间的随机数,要求不能重复
import java.util.ArrayList;
import java.util.Random;
/*
* Random类: 产生一个随机数生成器
* 成员方法 public int nextInt(n) {} [0,n)
* 分析:
* 1)创建一个随机数生成器对象:Random() {}
* 2)定义一个统计变量count
* 3)可以使用数组,数组长度固定----->使用集合ArrayList<Integer> 创建该集合对象
* 4)如果while(count < 10)
* 通过随机数生成器对象产生一些随机数 public int nextInt(n)
* 还需判断集合中是否存在产生的随机数,如果存在,不管它
* 不存在的话,应该添加集合
* count++;
* 大于10,不管它
* 5)遍历集合
*
* */
public class ArrayListTest {
public static void main(String[] args) {
//创建一个随机数生成器对象
Random r = new Random() ;
//使用ArrayList集合存储数据
ArrayList<Integer> array = new ArrayList<Integer>() ;
//定义一个统计变量
int count = 0 ;
//满足条件:获取10个随机数
while(count < 10) {
//通过随机数生成器对象产生一些随机数 public int nextInt(n)
int number = r.nextInt(20) +1 ;
//还需判断集合中是否存在产生的随机数,如果不存在,将随机数添加到集合中
//存在,重复了--->不搭理它
if(!array.contains(number)) {
//添加随机数进入集合
array.add(number) ;
count ++ ;
}
}
//遍历集合获取数据
for(Integer i:array) {
System.out.println(i);
}
}
}
3、需求:键盘录入多个数据,以0结束,要求在控制台输出这多个数据中的最大值
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/*
* 使用集合完成---->长度可变
* 1)创建一个集合ArrayList<Integer>
* 2)创建键盘录入对象录入数据
* 3)不知道循环次数while(true){}
*
* 4)如果没有输入0---->将录入int数据添加到集合中
* 如果是输入0,break--->结束循环
* 5)将集合转换成数组中
* toArray()---->Integer[] i
* Integer[] ii = new Integer[集合对象.size()] ;
*
* 6)排序----->Arrays.sort(ii); [从小到到]
* 7)将Intege[]数组--->遍历
* [元素1,元素2,元素3,]
*
* */
public class ArrayListTest2 {
public static void main(String[] args) {
//创建一个集合对象
ArrayList<Integer> array = new ArrayList<Integer>() ;
while(true) {
//创建键盘录入对象
Scanner sc = new Scanner(System.in) ;
//提示并接收
System.out.println("请您输入数据:");
int num = sc.nextInt() ;
//判断是否输入的0
if(num!=0) {
//将元素添加到集合中
array.add(num) ;
}else {
break ;//结束循环
}
}
//将ArrayList转换成数组
//toArray()
//Integer[] i = new Integer[长度] ;
Integer[] i = new Integer[array.size()] ; //new Integer[5]
//<T> T[] toArray(T[] a)
Integer[] ii = array.toArray(i) ;
//Integer[] i = (Integer[]) array.toArray() ;
//集合转换成数组
//public static void sort(Object[] a)
Arrays.sort(ii); //sort(int[] arr)
//获取最大值的同时,查看数组的元素内容
System.out.println("数组是:"+Arrays.toString(ii)+",最大值是:"+
ii[ii.length-1]);
//System.out.println(Arrays.toString(i)); //将数组拼接成字符串 [元素1,元素2...]
}
// public static String arrayToString(Integer[] i) {
// //创建一个字符串缓冲区对象
// StringBuffer sb = new StringBuffer() ;
// sb.append("[") ;
// for(int x = 0 ; x < i.length ; x ++) {
// if(x==i.length-1) {
// sb.append(i) ;
// sb.append("]");
// }else {
// sb.append(i+", ");
// }
// }
// return sb.toString();
// }
}