集合:
ArrayList 去除集合中字符串的重复值(字符串的内容相同)
- package com.heima.list;
- import java.util.ArrayList;
- import java.util.Iterator;
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public class Demo1_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("c");
- list.add("c");
- list.add("c");
- list.add("c");
- ArrayList newList = getSingle(list);
- System.out.println(newList);
- }
- /*
- * 创建新集合将重复元素去掉
- * 1, 明确返回值类型, 返回 ArrayList
- * 2, 明确参数列表 ArrayList
- *
- * 分析:
- * 1, 创建新集合
- * 2, 根据传入的集合 (老集合) 获取迭代器
- * 3, 遍历老集合
- * 4, 通过新集合判断是否包含老集合中的元素, 如果包含就不添加, 如果不包含就添加
- */
- public static ArrayList getSingle(ArrayList list) {
- ArrayList newList = new ArrayList<>(); //1, 创建新集合
- Iterator it = list.iterator(); //2, 根据传入的集合 (老集合) 获取迭代器
- while(it.hasNext()) { //3, 遍历老集合
- Object obj = it.next(); // 记录住每一个元素
- if(!newList.contains(obj)) { // 如果新集合中不包含老集合中的元素
- newList.add(obj); // 将该元素添加
- }
- }
- return newList;
- }
- }
需求: ArrayList 去除集合中自定义对象元素的重复值(对象的成员变量值相同)
- package com.heima.list;
- import java.util.ArrayList;
- import java.util.Iterator;
- import com.heima.bean.Person;
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public class Demo2_ArrayList {
- /**
- * * A: 案例演示
- * 需求: ArrayList 去除集合中自定义对象元素的重复值(对象的成员变量值相同)
- * B: 注意事项
- * 重写 equals()方法的
- contains 方法判断是否包含, 底层依赖的是 equals 方法
- remove 方法判断是否删除, 底层依赖的是 equals 方法
- */
- 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);
- list.remove(new Person("张三", 23));
- System.out.println(list);
- }
- /*
- * 创建新集合将重复元素去掉
- * 1, 明确返回值类型, 返回 ArrayList
- * 2, 明确参数列表 ArrayList
- *
- * 分析:
- * 1, 创建新集合
- * 2, 根据传入的集合 (老集合) 获取迭代器
- * 3, 遍历老集合
- * 4, 通过新集合判断是否包含老集合中的元素, 如果包含就不添加, 如果不包含就添加
- */
- public static ArrayList getSingle(ArrayList list) {
- ArrayList newList = new ArrayList<>(); //1, 创建新集合
- Iterator it = list.iterator(); //2, 根据传入的集合 (老集合) 获取迭代器
- while(it.hasNext()) { //3, 遍历老集合
- Object obj = it.next(); // 记录住每一个元素
- if(!newList.contains(obj)) { // 如果新集合中不包含老集合中的元素
- newList.add(obj); // 将该元素添加
- }
- }
- return newList;
- }
- }
- 4
- public class Demo4_LinkedList {
- /**
- * 用 LinkedList 模拟栈结构
- */
- public static void main(String[] args) {
- //demo1();
- 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 static void demo1() {
- LinkedList list = new LinkedList(); // 创建集合对象
- list.addLast("a");
- list.addLast("b");
- list.addLast("c");
- list.addLast("d");
- /*System.out.println(list.removeLast());
- System.out.println(list.removeLast());
- System.out.println(list.removeLast());
- System.out.println(list.removeLast());*/
- while(!list.isEmpty()) {
- System.out.println(list.removeLast());
- }
- }
- }
- }
- public class Stack {
- private LinkedList list = new LinkedList();
- /*
- * 模拟进栈方法
- */
- public void in(Object obj) {
- list.addLast(obj);
- }
- /*
- * 模拟出栈
- */
- public Object out() {
- return list.removeLast();
- }
- /*
- * 模拟栈结构是否为空
- */
- public boolean isEmpty() {
- return list.isEmpty();
- }
- }
泛型概述
- import java.util.ArrayList;
- import java.util.Iterator;
- import com.heima.bean.Person;
- public class Demo1_Generic {
- /**
- * A: 泛型概述
- * B: 泛型好处
- * 提高安全性(将运行期的错误转换到编译期)
- * 省去强转的麻烦
- * C: 泛型基本使用
- * <>中放的必须是引用数据类型
- * D: 泛型使用注意事项
- * 前后的泛型必须一致, 或者后面的泛型可以省略不写(1.7 的新特性菱形泛型)
- */
- public static void main(String[] args) {
- //demo1();
- //int[] arr = new byte[5]; // 数组要保证前后的数据类型一致
- //ArrayList<Object> list = new ArrayList<Person>(); // 集合的泛型要保证前后的数据类型一致
- //ArrayList<Object> list = new ArrayList<>(); //1.7 版本的新特性, 菱形泛型
- ArrayList<Object> list = new ArrayList<>(); // 泛型最好不要定义成 Object, 没有意义
- list.add("aaa");
- list.add(true);
- }
- public static void demo1() {
- ArrayList<Person> list = new ArrayList<Person>();
- // list.add(110);
- // list.add(true);
- list.add(new Person("张三", 23));
- list.add(new Person("李四", 24));
- Iterator<Person> it = list.iterator();
- while(it.hasNext()) {
- //System.out.println(it.next());
- //System.out.println(it.next().getName() + "..." + it.next().getAge());//next 方法只能调用一次, 如果调用多次会将指针向后移动多次
- Person p = it.next();
- System.out.println(p.getName() + "..." + p.getAge());
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2654235.html