ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

集合

2022-01-07 12:35:43  阅读:147  来源: 互联网

标签:col3 System println add 集合 col1 out


代码1:

package com.atguigu.day15;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

/*
集合:存储多个数据的,长度不固定
数组:数组的长度是固定的

Collection
List:有序,按照添加的顺序,不唯一,可以添加重复的元素
      ArraysList
      LinkedList

Set:无序,不是按照添加的顺序输出,唯一,不可以添加重复元素
    HashSet
    LinkedHashSet
    TreeSet

* */
public class Demo10 {
    @Test
    public void test1(){
        //多态
        Collection col1 = new ArrayList();
        //添加元素
        col1.add(10);//内部Integer.valueof(10)
        col1.add(20);
        col1.add(new Date());
        System.out.println(col1);//[10, 20, Fri Jan 07 11:02:11 CST 2022]

        Collection col2 = new ArrayList();
        col2.add("张无忌");
        col2.add("赵敏");

        //add()会将一个集合作为一个元素处理
        col1.add(col2);
        System.out.println(col1);
        //[10, 20, Fri Jan 07 11:08:01 CST 2022, [张无忌, 赵敏]]

        //addAll()会将集合中的每一个元素进行单独处理
        col1.addAll(col2);
        System.out.println(col1);
        //[10, 20, Fri Jan 07 11:08:01 CST 2022, [张无忌, 赵敏], 张无忌, 赵敏]

        System.out.println(col1.size());//长度为6

        //判断元素是否在集合内
        boolean isExsit = col2.contains("张无忌");
        System.out.println(isExsit);//true

        //清除指定元素
        col1.remove(10);
        System.out.println(col1);//[20, Fri Jan 07 11:15:26 CST 2022, [张无忌, 赵敏], 张无忌, 赵敏]

        //删除集合
        col1.removeAll(col2);
        System.out.println(col1);//[20, Fri Jan 07 11:15:26 CST 2022, [张无忌, 赵敏]]


        Collection col3 = new ArrayList();
        col3.add(10);
        col3.add(20);
        col3.add(30);

        Collection col4 = new ArrayList();
        col4.add(10);
        col4.add(20);

        col3.retainAll(col4);
        System.out.println(col3);//[10, 20]

        //判断集合是否为空
        System.out.println("col3.isEmpty() = " + col3.isEmpty());//col3.isEmpty() = false

        //清除集合所有元素
        col3.clear();
        System.out.println(col3);//[]

    }

    @Test
    public void test2(){
        Collection col1 = new ArrayList();
        col1.add(10);
        col1.add(20);
        col1.add(30);
        col1.add("张无忌");

        //遍历集合第一种方法
        for (Object ojb:col1){
            System.out.println(ojb);
        }

        //遍历集合第二种方法,通过创建迭代器实现
        //hasNext()判断游标后面是否还有数据
        //next()拿到指定的数据
        Iterator iterator = col1.iterator();
        while (iterator.hasNext()){
            Object obj=iterator.next();
            System.out.println(obj);
        }

        //转换类型
        Collection col2 = new ArrayList();
        col2.add("abc");
        col2.add("efg");
        col2.add("bpf");

        for (Object obj:col2){
            String s = (String)obj;
            System.out.println(s);
        }

        System.out.println("-------------------");
        //泛型:规定你输入数据的类型   Collection<引用数据类型>
        Collection<String> col3 = new ArrayList<>();
        col3.add("司马亮");
        col3.add("司马玮");
        col3.add("司马伦");
        col3.add("司马囧");

        for (String s:col3){
            if (s.equals("司马伦")){
               // col3.add("司马乂");//此时会报错,java.util.ConcurrentModificationException
            }
        }
        System.out.println(col3);

        System.out.println("-------------------");
        Iterator<String> iterator1 = col3.iterator();
        while (iterator1.hasNext()){
            String s = iterator1.next();
            if (s.equals("司马伦")){
                // col3.add("司马乂");//此时会报错,java.util.ConcurrentModificationException
                iterator1.remove();
            }
        }
        System.out.println(col3);
        /*modcount:记录集合的操作次数
          在进行集合遍历的时候,不要使用集合对象直接新增或者删除元素,应该使用iterator去操作
        */

    }

}

代码2:

 

package com.atguigu.day15;

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

public class Demo11 {
    public static void main(String[] args) {
        Collection<StudentTest> colTest = new ArrayList<>();
        StudentTest stu1 = new StudentTest("陆小凤",98);
        StudentTest stu2 = new StudentTest("西门吹雪",38);
        StudentTest stu3 = new StudentTest("摘星子",58);
        colTest.add(stu1);
        colTest.add(stu2);
        colTest.add(stu3);

        Iterator<StudentTest> iterator = colTest.iterator();
        while (iterator.hasNext()){
            StudentTest stu = iterator.next();
            if (stu.score<60){
                iterator.remove();
            }
        }

        System.out.println(colTest);
    }
}


class StudentTest{
    String name;
    int score;

    public StudentTest() {
    }

    public StudentTest(String name, int score) {
        this.name = name;
        this.score = score;
    }

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

输出:

[StudentTest{name='陆小凤', score=98}]

代码3:

标签:col3,System,println,add,集合,col1,out
来源: https://www.cnblogs.com/hbxZJ/p/15774568.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有