List集合嵌套并输出、List集合去重(并且空间复杂度要求O(n))

1、实现List的嵌套遍历(外层List中的元素是List对象 List list)。

package com.secondphase.homework.day02;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author jinman1012@qq.com   2020/8/12 19:37
 * @Version 1.0
 */
public class Problem1 {
    public static void main(String[] args) {
        //创建班级动态数组
        ArrayList<Student> classOne = new ArrayList<>();
        ArrayList<Student> classTwo = new ArrayList<>();
        //创建年级动态数组
        ArrayList<ArrayList<Student>> arrBig = new ArrayList<>();
        //一班
        Student stu1 = new Student("张三");
        Student stu2 = new Student("李四");
        Student stu3 = new Student("王五");
        Student stu4 = new Student("赵六");
        classOne.add(stu1);
        classOne.add(stu2);
        classOne.add(stu3);
        classOne.add(stu4);
        arrBig.add(classOne);
        //二班
        Student stu5 = new Student("龙狙");
        Student stu6 = new Student("二西");
        Student stu7 = new Student("泰坦");
        Student stu8 = new Student("咆哮");
        classTwo.add(stu5);
        classTwo.add(stu6);
        classTwo.add(stu7);
        classTwo.add(stu8);
        arrBig.add(classTwo);
        //建议将Iterator放for循环,并不是所有地方都需要用到迭代器,防止其他地方报错
        for(Iterator<ArrayList<Student>> iteBig = arrBig.iterator();iteBig.hasNext();) {
            for(Iterator<Student> iteSmall = iteBig.next().iterator();iteSmall.hasNext();){
                System.out.println(iteSmall.next());
            }
        }
    }
}
class Student {
    private String name;
    public Student(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

2、用 List 存储一些字符串,去除里面重复的字符串,只保留一个。

package com.secondphase.homework.day02;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author jinman1012@qq.com   2020/8/12 19:37
 * @Version 1.0
 */
public class Problem2 {
    public static void main(String[] args) {
        List list = new ArrayList<>(
        	Arrays.asList("qiezi", "pdd", "csgo", "pubg", "yjj", "qiezi", "qiezi"));
        System.out.println(list);
        //堆上的对象是同一个,不需要返回值
        removeSameEle(list);
        System.out.println(list);
    }

    public static void removeSameEle(List list) {
        if (list.isEmpty() || list.size() == 1) {
            return;
        }
        //for (int i = 0; i < list.size()-1; i++)效果也相同
        //第二个for同减了1的也进不去
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i).equals(list.get(j))) {
                    list.remove(j);
                    j--;
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值