泛型概述
去除ArrayList中重复字符串元素方式(掌握)
- A:案例演示
-
需求:ArrayList去除集合中字符串的重复值(字符串的内容相同)
-
思路:创建新集合方式
/** * A:案例演示 * 需求:ArrayList去除集合中字符串的重复值(字符串的内容相同) * 思路:创建新集合方式 */ public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("a"); list.add("a"); list.add("b"); list.add("b"); list.add("b"); list.add("c"); list.add("c"); list.add("c"); list.add("c"); System.out.println(list); ArrayList newList = getSingle(list); System.out.println(newList); } /* * 去除重复 * 1,返回ArrayList * 2,参数列表ArrayList */ public static ArrayList getSingle(ArrayList list) { ArrayList newList = new ArrayList(); //创建一个新集合 Iterator it = list.iterator(); //获取迭代器 while(it.hasNext()) { //判断老集合中是否有元素 String temp = (String)it.next(); //将每一个元素临时记录住 if(!newList.contains(temp)) { //如果新集合中不包含该元素 newList.add(temp); //将该元素添加到新集合中 } } return newList; //将新集合返回 } 输出结果:[a, a, b, b, b, c, c, c, c] [a, b, c]
-
去除ArrayList中重复自定义对象元素(掌握)
- A:案例演示
- 需求:ArrayList去除集合中自定义对象元素的重复值(对象的成员变量值相同)
- B:注意事项
-
重写equals()方法的
class Demo { public static void main(String[] args) { ArrayList list = new ArrayList(); //创建集合对象 list.add(new Person("张三", 23)); list.add(new Person("张三", 23)); list.add(new Person("李四", 24)); list.add(new Person("李四", 24)); list.add(new Person("李四", 24)); list.add(new Person("李四", 24)); ArrayList newList = getSingle(list); //调用方法去除重复元素 System.out.println(newList); } public static ArraryList getSingle(ArrayList list) { ArrayList newList = new ArrayList(); Iterator it = list.iterator(); //获取迭代器 while(it.hasNext()) { //判断老集合中是否有元素 Object obj = it.next(); //将每一个元素临时记录住 if(!newList.contains(obj)) { //如果新集合中不包含该元素 newList.add(obj); //将该元素添加到新集合中 } } return newList; } } class person { private String name; private int age; public Person() { super(); } public Person(String name,int age) { super(); this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } @Override public String toString() { return "Person [name=" + name +", age=" + age + "]"; } @Override public boolean equals(object obj) { Person p = (Person)obj; return this.name.equals(p.name) && this.age == p.age; } } 输出结果:[Person [name=张三, age=23], Person [name=李四, age=24]] 注意:contains方法判断是否包含,底层依赖的是equals方法 remove方法判断是否删除,底层依赖的是equals方法
-
LinkedList的特有功能)(掌握)
- LinkedList类概述
- LinkedList类特有功能
-
public void addFirst(E e)及addLast(E e)
LinkedList list = new LinkedList(); list.addFirst("a"); list.addFirst("b"); list.addFirst("c"); list.addFirst("d"); list.addLast("e"); System.out.println(list); 输出结果:[d, c, b, a, e] -
public E getFirst()及getLast()
LinkedList list = new LinkedList(); list.addFirst("a"); list.addFirst("b"); list.addFirst("c"); list.addFirst("d"); list.addLast("e"); System.out.println(list); System.out.println(list.getFirst()); System.out.println(list.getLast()); 输出结果:[d, c, b, a, e] d e -
public E removeFirst()及public E removeLast()
LinkedList list = new LinkedList(); list.addFirst("a"); list.addFirst("b"); list.addFirst("c"); list.addFirst("d"); list.addLast("e"); System.out.println(list.removeFirst()); System.out.println(list.removeLast()); System.out.println(list); 输出结果:d e [c, b, a] -
public E get(int index);
LinkedList list = new LinkedList(); list.addFirst("a"); list.addFirst("b"); list.addFirst("c"); list.addFirst("d"); list.addLast("e"); System.out.println(list.get(1)); System.out.println(list); 输出结果:c [d, c, b, a, e]
-
栈和队列数据结构(掌握)
- 栈
- 先进后出
- 队列
- 先进先出
- 先进先出
用LinkedList模拟栈数据结构的集合并测试(掌握)
- A:案例演示
-
需求:请用LinkedList模拟栈数据结构的集合,并测试
-
创建一个类将Linked中的方法封装
class stack_Demo { public static void main(String[] args) { Stack s = new Stack(); s.in("a"); //进栈 s.in("b"); s.in("c"); s.in("d"); while(!s.isEmpty()) { //判空 System.out.println(s.out()); //弹栈 } } } public class Stack { private LinkedList list = new LinkedList(); //创建LinkedList对象 public void in(Object obj) { list.addLast(obj); //封装addLast()方法 } public Object out() { return list.removeLast(); //封装removeLast()方法 } public boolean isEmpty() { return list.isEmpty(); //封装isEmpty()方法 } } 输出结果:d c b a
-
泛型概述和基本使用(掌握)
- A:泛型概述
- B:泛型好处
- 提高安全性(将运行期的错误转换到编译期)
- 省去强转的麻烦
- C:泛型基本使用
- <>中放的必须是引用数据类型
- D:泛型使用注意事项
-
前后的泛型必须一致,或者后面的泛型可以省略不写(1.7的新特性菱形泛型)
-
泛型最好不要定义为object,没有意义
* ArrayList<Object> list = new ArrayList<Person>(); //报错!集合的泛型要保持前后数据类型一致 public static void main(String[] args) { ArrayList<Person> list = new ArrayList<Person>(); list.add(new Person("张三", 23)); list.add(new Person("李四", 24)); Iterator<Person> it = list.iterator(); while(it.hasNext()) { Person p =it.next(); //将集合中的每一个元素用Person记录 System.out.println(p.getName() + "..." + p.getAge); } } 输出结果:张三...23 李四...24
-
Array存储字符串和定义对象并遍历泛型
import java.util.ArrayList;
import java.util.Iterator;
public class Demo_Generic {
public static void main(String [] args) {
ArrayList<String> list = new ArrayList<>(); //创建集合对象,后面可以不用写,会自动与前面的String对应,这是新特性
list.add("a");
list.add("b");
list.add("c");
list.add("d");
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
输出:
a
b
c
d
泛型的由来
- 由来:通过object转型问题引入
- 早期的object类型可以接收任意的对象类型,但是在实际的使用中,会有类型转换的问题。也正因如此,Java提供了泛型来解决这个安全问题。
泛型接口的概述和使用
-
泛型接口概述:把泛型定义在接口上
-
定义格式:public interface 接口名 <泛型类型>
-
演示
interface Inter<T> { public void show(T t); } //第一种方法,推荐使用 class Demo implements Inter<String> { public void show(String t) { System.out.println(t); } } //第二种方法,不推荐使用。没有必要再实现接口的时候给自己类加泛型 class Demo<T> implements Inter<T> { public void show(T t) { System.out.println(t); } }
泛型高级之通配符
- 泛型通配符<?>
- ?代表任意类型,如果没有明确,那么就是object以及任意的Java类了
List<?> list = new ArrayList
(); //当右边的泛型不确定的时候,左边可以指定为?
-
? extends E
-
向下限定,E及其子类
ArrayList<Person> list = new ArrayList<>(); list1.add(new Person("张三", 23)); list1.add(new Person("李四", 24)); list1.add(new Person("王五", 25)); ArrayList<Student> list = new ArrayList<>(); list2.add(new Student("赵六", 26)); list2.add(new Student("周七", 27)); list1.addAll(list2); // 子类自动提升为父类 System.out.println(list1); //可以编译成功 list2.addAll(list1); // 编译出错。父类不能提升为子类
-
-
? super E
- 向上限定,E及其父类
- 向上限定,E及其父类
增强for的概述和使用
-
概述:简化数组collection集合的遍历
-
好处:遍历数组
public static void main(String [] args) { int[] arr = {11,22,33,44,55}; for (int i : arr) { System.out.println(i); } } 输出: 11 22 33 44 55
import java.util.ArrayList;
public class Test {
public static void main(String [] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for (String string : list ) {
System.out.println(string);
}
}
}
输出:
a
b
c
d
三种迭代能否删除
普通for循环删除,索引要--
import java.util.ArrayList;
public class Test {
public static void main(String [] args) {
ArrayList
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for (int i = 0 ; i < list.size(); i ++) {
if("b".equals(list.get(i))) {
list.remove(i);
}
}
System.out.println(list);
}
}
输出: [a, c, d]
************************************************
import java.util.ArrayList;
public class Test {
public static void main(String [] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
for (int i = 0 ; i < list.size(); i ++) {
if("b".equals(list.get(i))) {
list.remove(i);
}
}
System.out.println(list);
}
}
输出:[a, b, c, d]
**************************************************
import java.util.ArrayList;
public class Test {
public static void main(String [] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
for (int i = 0 ; i < list.size(); i ++) {
if("b".equals(list.get(i))) {
list.remove(i--);
}
}
System.out.println(list);
}
}
输出:[a, c, d]
**************************************************
import java.util.ArrayList;
public class Test {
public static void main(String [] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("b");
list.add("d");
for (int i = 0 ; i < list.size(); i ++) {
if("b".equals(list.get(i))) {
list.remove(i);
}
}
System.out.println(list);
}
}
输出:[a, c, d]
迭代器删除
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String [] args) {
ArrayList<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("b");
list.add("d");
Iterator<String> it = list.iterator();
/* while(it.hasNext()) {
if ("b".equals(it.next())) {
// list.remove("b"); 不能用集合的删除方法,因为迭代过程如果集合修改会出现并发修改异常
it.remove();
}
}
效果同下
*/
for(Iterator<String> it2 = list.iterator(); it2.hasNext(); ) {
if("b".equals(it2.next())) {
it2.remove();
}
}
System.out.println(list);
}
}
输出:[a, c, d]
增强for循环,不能删除,只能遍历
静态导入的概述和使用方法(开发中一般不用)
- 概述:静态导入时导入类中静态方法
- 格式:import static 包名.类名.方法名;(可以直接导入到方法级别)
- 注意事项:方法必须是静态的,如果有多个同名的静态方法,容易不知道使用谁,这个时候要使用,必须加前缀。由此可见,意义不大。
静态导入不用,但是要能看懂,有人可能会用
可变参数的概述和使用
- 概述:定义方法的时候不知道该定义多少个参数
- 格式:修饰符 返回值类型 方法名 (数据类型... 变量名) {}
- 注意事项:
-
这里的变量其实是一个数组
-
如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
public static void main(String[] args) { int[] arr = {11,22,33,44,55}; print(arr); } /* * public static void print(int [] arr) { * for (int i = 0; i < arr.length; i ++) { * System.out.println(arr[i]); * } * } * 输出: * 11 * 22 * 33 * 44 * 55 */ public static void print(int ... arr) { for (int i = 0; i < arr.length; i ++) { System.out.println(arr[i]); } } 输出: 11 22 33 44 55 }
-
public static void main(String[] args) {
print(11,22,33,44,55);
}
/*
* public static void print(int [] arr) {
* for (int i = 0; i < arr.length; i ++) {
* System.out.println(arr[i]);
* }
* }
* 报错!
*/
public static void print(int ... arr) {
for (int i = 0; i < arr.length; i ++) {
System.out.println(arr[i]);
}
}
//在主方法中写print();也不会报错,输出为空
输出:
11
22
33
44
55
}
public static void main(String[] args) {
print(11,22,33,44,55);
}
public static void print(int x, int ... arr) {
for (int i = 0; i < arr.length; i ++) {
System.out.println(arr[i]);
}
}
输出://把11给了int x
22
33
44
55
}
public static void main(String[] args) {
print(11,22,33,44,55);
}
public static void print( int ... arr, int x) {
for (int i = 0; i < arr.length; i ++) {
System.out.println(arr[i]);
}
}
输出:报错!他会默认把11,22,33,44,55看成一个数组给int ... arr了,而int x 没有值传入
}
// 所以:如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个
Arrays工具类的asList()方法的使用
集合嵌套之ArrayList嵌套ArrayList
package first_1;
import number1.Person;
import java.util.ArrayList;
/**
* 集合嵌套之ArrayList嵌套ArrayList
* 案例:
* 我们学科,学科有分为若干个班级
* 整个学科为一个大集合
* 若干个班级分为每一个小集合
*
*/
public class test02 {
public static void main(String[] args) {
ArrayList<ArrayList<Person>> list = new ArrayList<>();
ArrayList<Person> first = new ArrayList<>();
first.add(new Person("杨幂", 30));
first.add(new Person("方密", 30));
first.add(new Person("李冰冰", 30));
ArrayList<Person> second = new ArrayList<>();
second.add(new Person("黄晓明", 31));
second.add(new Person("陈坤", 30));
second.add(new Person("杨建行", 33));
//将班级添加到学科集合中
list.add(first);
list.add(second);
//遍历学科集合
for(ArrayList<Person> a : list) {
for(Person p : a) {
System.out.println(p);
}
}
}
}
*******************************************
package number1;
public class Person {
public String name;
public int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public String toString() {
return "Person [name = "+ name +" , age = "+ age +"]";
}
}

浙公网安备 33010602011771号