泛型概述
去除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
-

浙公网安备 33010602011771号