学习笔记

xiaoxiao2021-02-28  57

1:List的子类(掌握) (1)List的子类特点 ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector: 底层数据结构是数组,查询快,增删慢 线程安全,效率低 LinkedList: 底层数据结构是链表,查询慢,增删快 线程不安全,效率高 (2)ArrayList A:没有特有功能需要我们学习 B:案例 a:ArrayList存储字符串并遍历 // 创建集合对象 ArrayList array = new ArrayList(); // 创建元素对象,并添加元素 array.add("hello"); array.add("world"); array.add("java"); // 遍历 //迭代器 Iterator it = array.iterator(); while (it.hasNext()) { String s = (String) it.next(); System.out.println(s); } System.out.println("-----------"); //普通for循环  size()和get()方法 for (int x = 0; x < array.size(); x++) { String s = (String) array.get(x); System.out.println(s); } b:ArrayList存储自定义对象并遍历 // 创建集合对象 ArrayList array = new ArrayList(); // 创建学生对象 Student s1 = new Student("武松", 30); Student s2 = new Student("鲁智深", 40); Student s3 = new Student("林冲", 36); Student s4 = new Student("杨志", 38); // 添加元素 array.add(s1); array.add(s2); array.add(s3); array.add(s4); // 遍历 //迭代器 Iterator it = array.iterator(); while (it.hasNext()) { Student s = (Student) it.next(); System.out.println(s.getName() + "---" + s.getAge()); } System.out.println("----------------"); //普通for循环  size()和get()方法 for (int x = 0; x < array.size(); x++) { // ClassCastException 注意,千万要搞清楚类型 // String s = (String) array.get(x); // System.out.println(s); Student s = (Student) array.get(x); System.out.println(s.getName() + "---" + s.getAge()); } (3)Vector A:有特有功能  现在已经不用了 a:添加 public void addElement(E obj) -- 被add()替代 b:获取 public E elementAt(int index) -- 被get()替代 public Enumeration<E> elements() --  被iterator()替代 -- hasNext() -- next() B:案例  自己练习 a:Vector存储字符串并遍历 b:Vector存储自定义对象并遍历 (4)LinkedList A:有特有功能 a:添加 addFirst() 在开头添加元素e 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:集合的嵌套遍历 B:产生10个1-20之间的随机数,要求随机数不能重复 C:键盘录入多个数据,以0结束,并在控制台输出最大值。 7:要掌握的代码 集合存储元素,加入泛型,并可以使用增强for遍历。 /* * ArrayList存储字符串并遍历。要求加入泛型,并用增强for遍历。 * A:迭代器 * B:普通for * C:增强for */ public class ArrayListDemo { public static void main(String[] args) { // 创建集合对象 ArrayList<String> array = new ArrayList<String>(); // 创建并添加元素 array.add("hello"); array.add("world"); array.add("java"); // 遍历集合 // 1.迭代器 Iterator<String> it = array.iterator(); while (it.hasNext()) { String s = it.next(); System.out.println(s); } System.out.println("------------------"); // 2.普通for for (int x = 0; x < array.size(); x++) { String s = array.get(x); System.out.println(s); } System.out.println("------------------"); // 3.增强for   以后我们都用这个 for (String s : array) { System.out.println(s); } } } /* * 需求:ArrayList存储自定义对象并遍历。要求加入泛型,并用增强for遍历。 * A:迭代器 * B:普通for * C:增强for * LinkedList,Vector,Colleciton,List等存储我还讲吗?不讲解了,但是要求你们练习。 * 增强for是用来替迭代器。 */ public class ArrayListDemo2 { public static void main(String[] args) { // 创建集合对象 ArrayList<Student> array = new ArrayList<Student>(); // 创建学生对象 Student s1 = new Student("林青霞", 27); Student s2 = new Student("貂蝉", 22); Student s3 = new Student("杨玉环", 24); Student s4 = new Student("西施", 21); Student s5 = new Student("王昭君", 23); // 把学生对象添加到集合中 array.add(s1); array.add(s2); array.add(s3); array.add(s4); array.add(s5); // 1迭代器 Iterator<Student> it = array.iterator(); while (it.hasNext()) { Student s = it.next(); System.out.println(s.getName() + "---" + s.getAge()); } System.out.println("---------------"); // 2普通for for (int x = 0; x < array.size(); x++) { Student s = array.get(x); System.out.println(s.getName() + "---" + s.getAge()); } System.out.println("---------------"); // 3增强for for (Student s : array) { System.out.println(s.getName() + "---" + s.getAge()); } } }
转载请注明原文地址: https://www.6miu.com/read-2620234.html

最新回复(0)