linkedlist

1.ArrayList

  去除ArrayList中重复字符串元素方式

  同一个循环中,不能有两个next()方法,否则数据会丢失

 1     /*
 2          * 需求:去除ArrayList集合重复的元素
 3          * 1.创建一个新集合,
 4          * 2.使用迭代器,把原集合的元素添加进新集合,
 5          * 3.如果新集合中已经有了的元素就不添加了
 6          */
 7         List list = new ArrayList();
 8         list.add("a");
 9         list.add("a");
10         list.add("b");
11         list.add("c");
12         list.add("c");
13         list.add("c");
14         
15         //创建一个新集合
16         ArrayList alist =new ArrayList(); 
17         
18         //迭代器
19         Iterator it= list.iterator();
20         while (it.hasNext()) {
21             
22             //把原集合转为String类型,一个循环中不能拥有两个next,
23             String str = (String)it.next();
24             
25             //判断新集合中的元素和原集合元素有没有重复的
26             //如果有重复的就不添加了
27             if (!(alist.contains(str))) {
28                 alist.add(str);     
29             }
30         }
31         System.out.println(alist);

  

   去除重复自定义对象

 

 1         /*
 2          * 去除重复自定义对象
 3          * 重要是的自定义类中要记得重写equals方法
 4          */
 5         ArrayList list = new ArrayList();
 6         list.add(new Student("z",34));
 7         list.add(new Student("ww",43));
 8         list.add(new Student("r",12));
 9         list.add(new Student("z",34));
10         //创建新集合
11         ArrayList list2 = new ArrayList<>();
12         //迭代器
13         Iterator it = list.iterator();
14         while(it.hasNext()) {
15             //向下转型到学生类
16             Student stu = (Student)it.next();
17             //没有相同的就添加
18             if (!(list2.contains(stu))) {
19                 list2.add(stu);
20             }
21         }
22         System.out.println(list2);




 

 2.linkedList集合

  

 1     LinkedList linked = new LinkedList();
 2         //添加到集合元素的最前面
 3         linked.addFirst("a");
 4         linked.addFirst("b");
 5         //添加到集合元素的最后面
 6         linked.addLast("c");
 7         linked.addLast("v");
 8         linked.addLast("w");
 9         
10         System.out.println(linked);
11         //get()方法获取指定位置的元素
12         linked.get(3);
13         //删除第一个元素
14         linked.removeFirst();
15         //删除最后一个元素
16         linked.removeLast();
17         //获取最后一个元素
18         linked.getLast();
19         //获取第一个元素
20         linked.getFirst();
21         
22         
23         //遍历linkedList集合的迭代器
24         Iterator it = linked.iterator();
25         while(it.hasNext()) {
26             System.out.println(it.next());
27         }

 

 

3.泛型概述和基本使用

  泛型就是限定了集合中存储的数据类型

  好处:提高安全性(将运行期错误转到编译器错误)

     省区强转的麻烦

  泛型的基本使用:<>中放的必须是引用数据类型

  前后的泛型必须一致,或者后面的泛型可以省略不写

   

 1     // 泛型后的遍历迭代
 2         ArrayList list = new ArrayList();
 3         list.add(new Student("z", 34));
 4         list.add(new Student("ww", 43));
 5         list.add(new Student("r", 12));
 6         list.add(new Student("z", 34));
 7 
 8         Iterator<Student> it = list.iterator();
 9         while (it.hasNext()) {
10             Student str = it.next();
11 
12             System.out.println(str);
13             /*
14              * 输出结果为
15              * Student [name=z, age=34] 
16              * Student [name=ww, age=43] 
17              * Student [name=r,age=12] 
18              * Student [name=z, age=34]
19              */
20             System.out.println(str.getAge()+ " " + str.getName());
21             /*
22              * 输出结果为:
23              * 43 ww
24              * 12 r
25              * 34 z
26              */
27         }

 

 4.foreach的遍历方法

 1 ArrayList<String> arrayList = new ArrayList<>();
 2         arrayList.add("a");
 3         arrayList.add("b");
 4         arrayList.add("c");
 5         // 普通for遍历方式
 6         for (int i = 0; i < arrayList.size(); i++) {
 7             System.out.print(arrayList.get(i) + " ");
 8         }
 9         System.out.println("++++++++++++++");
10         // 迭代器遍历
11         Iterator it = arrayList.iterator();
12         while (it.hasNext()) {
13             System.out.print(it.next() + " ");
14         }
15         System.out.println("-------------------");
16         // 增强for循环
17         for (String s : arrayList) {
18             System.out.print(s + " ");
19         }

 

 

5.三种迭代方法是否可以删除

  可以使用增强for循环删除

 1         ArrayList<String> arrayList = new ArrayList<>();
 2         arrayList.add("a");
 3         arrayList.add("b");
 4         arrayList.add("g");
 5         // 普通for遍历方式
 6         for(int i=0; i<arrayList.size();i++){
 7             if ("b".equals(arrayList.get(i))) {
 8                 arrayList.remove(i--);
 9             }
10         }    

 

 6,集合嵌套

 

 1         //第一个班级,学习大数据
 2         ArrayList<Student> list2 = new ArrayList();
 3         list2.add(new Student("z", 34));
 4         list2.add(new Student("ww", 43));
 5         list2.add(new Student("r", 12));
 6         list2.add(new Student("z", 34));
 7         //第二个班级学习人工智能
 8         ArrayList<Student> list1 = new ArrayList();
 9         list1.add(new Student("z", 34));
10         list1.add(new Student("ww", 43));
11         list1.add(new Student("r", 12));
12         list1.add(new Student("z", 34));
13         //这是学校包扩班级
14         ArrayList<ArrayList<Student>> list = new ArrayList<>();
15         
16         //外循环控制班级
17         for (ArrayList<Student> a : list) {
18             //内循环控制输出学生
19             for (Student s : a) {
20                 System.out.println(s);
21             }
22         }

 

 7.增强for循环

    简化数组和Collection集合的遍历

  格式:

    for(元素数据类型 变量 : 数组或者Collection集合) {

        使用变量即可,该变量就是元素

    }

 

  简化遍历

 

 1 for (Student s : a) {

2   System.out.println(s);

3 }  

 

转载于:https://www.cnblogs.com/xsh726/p/11413011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值