Java学习第十六天

时间:2024-10-13 00:07:31
1:List的子类(掌握)
(1)List的子类特点
ArrayList:
底层数据结构是数组,查询快,增删慢
线程不安全,效率高
Vector:
底层数据结构是数组,查询快,增删慢
线程安全,效率低
LinkedList:
底层数据结构是链表,查询慢,增删快
线程不安全,效率高
(2)ArrayList
A:没有特有功能需要学习
B:案例
a:ArrayList存储字符串并遍历
b:ArrayList存储自定义对象并遍历
(3)Vector
A:有特有功能
a:添加
public void addElement(E obj) -- add()
b:获取
public E elementAt(int index) -- get()
public Enumeration<E> elements() -- iterator()
B:案例
a:Vector存储字符串并遍历
b:Vector存储自定义对象并遍历
(4)LinkedList
A:有特有功能
a:添加
addFirst()
addLast()
b:删除
removeFirst()
removeLast()
c:获取
getFirst()
getLast()
B:案例
a:LinkedList存储字符串并遍历
b:LinkedList存储自定义对象并遍历
(5)案例:
A:去除集合中的多个字符串的重复元素
如果字符串的内容相同,即为重复元素
B:去除集合中的多个自定义对象的重复元素
如果自定义对象的成员变量值都相同,即为重复元素
C:用LinkedList模拟一个栈数据结构的集合类,并测试。
你要定义一个集合类,只不过内部可以使用LinkedList来实现。 2:泛型(掌握)
(1)泛型概述
是一种把明确类型的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型。
(2)格式:
<数据类型>
注意:该数据类型只能是引用类型。
(3)好处:
A:把运行时期的问题提前到了编译期间
B:避免了强制类型转换
C:优化了程序设计,解决了黄色警告线问题,让程序更安全
(4)泛型的前世今生
A:泛型的由来
Object类型作为任意类型的时候,在向下转型的时候,会隐含一个转型问题
B:泛型类
C:泛型方法
D:泛型接口
E:泛型高级通配符
?
? extends E
? super E
(5)我们在哪里使用呢?
一般是在集合中使用。 3:增强for循环(掌握)
(1)是for循环的一种
(2)格式:
for(元素的数据类型 变量名 : 数组或者Collection集合的对象) {
使用该变量即可,该变量其实就是数组或者集合中的元素。
}
(3)好处:
简化了数组和集合的遍历
(4)弊端
增强for循环的目标不能为null。建议在使用前,先判断是否为null。 4:静态导入(了解)
(1)可以导入到方法级别的导入
(2)格式:
import static 包名....类名.方法名;
(3)注意事项:
A:方法必须是静态的
B:如果多个类下有同名的方法,就不好区分了,还得加上前缀。
所以一般我们并不使用静态导入,但是一定要能够看懂。 5:可变参数(掌握)
(1)如果我们在写方法的时候,参数个数不明确,就应该定义可变参数。
(2)格式:
修饰符 返回值类型 方法名(数据类型... 变量) {} 注意:
A:该变量其实是一个数组名
B:如果一个方法有多个参数,并且有可变参数,可变参数必须在最后
(3)Arrays工具类的一个方法
asList()把数组转成集合。
注意:这个集合的长度不能改变。 6:练习(掌握)
A:集合的嵌套遍历
import java.util.ArrayList; public class ArrayListDemo1 {
public static void main(String[] args) {
//创建大集合
ArrayList<ArrayList<Student>> stu = new ArrayList<ArrayList<Student>>();
//创建第一个班级的学生集合
ArrayList<Student> first = new ArrayList<Student>();
ArrayList<Student> second = new ArrayList<Student>();
ArrayList<Student> third = new ArrayList<Student>();
//创建学生
Student s1 = new Student("a1",12);
Student s2 = new Student("a2",13);
Student s3 = new Student("a3",14);
Student s4 = new Student("a4",15);
Student s5 = new Student("a5",16);
Student s6 = new Student("b1",17);
Student s7 = new Student("b2",18);
Student s8 = new Student("b3",19);
Student s9 = new Student("b4",20);
Student s10 = new Student("c1",21);
Student s11 = new Student("c2",22);
Student s12 = new Student("c3",23);
//学生进班
first.add(s1);
first.add(s2);
first.add(s3);
first.add(s4);
first.add(s5); second.add(s6);
second.add(s7);
second.add(s8);
second.add(s9); third.add(s10);
third.add(s11);
third.add(s12); //把班级学生存储到学籍系统中
stu.add(first);
stu.add(second);
stu.add(third); //遍历
for(ArrayList<Student> s:stu){
for(Student x:s){
System.out.println(x.getName()+"---"+x.getAge());
}
} }
}
B:产生10个1-20之间的随机数,要求随机数不能重复
import java.util.ArrayList;
import java.util.Random; /*
* 获取10个1-20之间的随机数要求不能重复
* 分析:
* 创建产生随机数的对象
* 创建一个存储随机数的集合
* 定义一个统计变量
* 判断统计变量是否小于10
* 是:判断随机数是否存在
* 存在:不搭理
* 不存在:统计变量++
* 否:退出循环
*/
public class RandomDemo {
public static void main(String[] args) {
Random r = new Random();
ArrayList<Integer> array = new ArrayList<Integer>();
int count = 0;
while(count<10){
int number = r.nextInt(20) + 1;
if(!array.contains(number)){
count++;
array.add(number);
}
} for(Integer y:array){
System.out.println(y);
}
}
}
C:键盘录入多个数据,以0结束,并在控制台输出最大值
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/*
* 键盘录入多个数据,以0结束,在控制台输出最大值
* 分析:
* 创建键盘录入对象
* 键盘录入多个数据,不知道多少个,所以用集合存储
* 以0结束,判断录入的数据为0时就不再录入数据了
* 把集合转为数组
* 对数组排序
* 获取最大索引的值即为最大值
*/
public class ArgsDemo {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in); while(true){
System.out.println("请输入数据:");
int number = sc.nextInt();
if(number != 0){
arr.add(number);
}else{
break;
}
} Integer[] i = new Integer[arr.size()];
arr.toArray(i);
Arrays.sort(i);
System.out.println("数组是:"+arrayToString(i)+"最大值是:"+i[i.length-1]); } public static String arrayToString(Integer[] i){
StringBuilder sb = new StringBuilder(); sb.append("["); for(int x=0;x<i.length;x++){
if(x == i.length-1){
sb.append(i[x]);
}else{
sb.append(i[x]).append(",");
}
}
sb.append("]"); return sb.toString(); } } 7:要掌握的代码
集合存储元素,加入泛型,并可以使用增强for遍历。