首页 > 编程语言 >java_day12

java_day12

时间:2022-10-14 15:23:52浏览次数:45  
标签:java list System add day12 println col out

Java基础

Java集合框架

什么是集合?

​ 对象的容器,定义了对多个对象进程操作的常用方法。可实现数组的功能。

与数组的区别是什么?

1. 数组长度固定,集合长度是不固定
1. 数组可以存储基本类型和引用类型,而集合只能存储引用类型

Collection体系集合

​ Collection ----Interface

​ List Set ----Interface

​ ArrayList LinkedList Vector HashSet SortedSet ----Class

​ TreeSet ----Class

Collection是此体系结构的根接口,代表一组对象,称为集合

List接口的特点是 有序、有下标、元素可以重复可以重复可以重复

Set接口的特点是 无序、无下标、元素不可重复不可重复不可重复

  • Collection父接口

    • 特点:代表一组任意类型的对象,无序、无下标、不能重复
    public class Demo1 {
    
        //Collection接口的使用
    
        public static void main(String[] args) {
    
            //创建一个集合
            Collection col = new ArrayList();
    
            //添加元素
             col.add("apple");
             col.add("watermelon");
             col.add("bannana");
             col.add("pear");
            System.out.println("元素个数:"+ col.size());
            System.out.println(col);
    
            //删除元素
            col.remove("bannana");
            System.out.println("删除后元素个数:"+ col.size());
    //        col.clear();//清空元素
    
            //遍历元素
            for (Object obj:col) {
                System.out.println(obj);
            }
    
            System.out.println("=================");
    
            Iterator it = col.iterator();//使用迭代器遍历集合
            /*
                迭代器是专门用来遍历的一种方式
                hasNext() 判断有无下一个元素
                next() 获取下一个元素
                remove() 删除当前元素
             */
    
            while (it.hasNext()){
    
                    System.out.println(it.next());
    //                it.remove();
    
            }
    //        System.out.println(col.size());
    
            //判断
            System.out.println(col.contains("pear"));//判断是否包含pear
            System.out.println(col.isEmpty());//判断是否为空
    
        }
    }
    
public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

public class TestStudent {
    public static void main(String[] args) {
        Student stu1 = new Student("zhangsan",18);
        Student stu2 = new Student("lisi",19);
        Student stu3 = new Student("wangwu",20);
        Student stu4 = new Student("laoliu",25);


        Collection col = new ArrayList();

        //添加student对象至集合中
        col.add(stu1);
        col.add(stu2);
        col.add(stu3);
        col.add(stu4);

        System.out.println("元素个数:"+col.size());
        System.out.println(col);//这里还是会调用重写的toString方法

        //删除元素
        System.out.println("由于张三太狂了要开除");
        col.remove(stu1);
        System.out.println(col.size());
        System.out.println(col.toString());//这里可以法toString是灰色的意味这即使不写也会隐式调用

        //遍历
        System.out.println("++++++++++++++++++++++");
        for (Object x:col){
            System.out.println(x);   //这里同样会调用Student的toString方法
        }


        System.out.println("==========================");
        Iterator it = col.iterator();
        while (it.hasNext()){
            //同理 next()返回的是Object类上上面一致
            System.out.println(it.next());
        }

        //判断
        System.out.println(col.isEmpty());
        System.out.println(col.contains("张三"));

    }
}

List子接口

  • 特点:有序、有下标 、元素可以重复

  • 方法:add、allAll、get、subList、

    public class Demo1 {
        public static void main(String[] args) {
            //创建List集合
            List list = new ArrayList<>();
    
            //添加元素
            list.add("a");
            list.add("b");
            list.add("c");
            list.add("d");
            list.add(1,"d");
            System.out.println("元素个数:"+list.size());
            System.out.println(list.toString());
    
            //删除
            list.remove("d");//删除顺序靠前的元素d
            list.remove("d");
            list.remove(1);//删除序列为1的元素
            System.out.println("删除后元素个数:"+list.size());
            System.out.println(list.toString());
    
            System.out.println("========================");
    
            //遍历
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
    
            System.out.println("foreach");
            for (Object obj : list) {
                System.out.println(obj);
            }
    
            System.out.println("Iterator");
            Iterator it = list.iterator();
            while (it.hasNext()){
                System.out.println(it.next());
            }
    
            System.out.println("ListIterator顺序");
            ListIterator it2 = list.listIterator();
            while (it2.hasNext()){
                System.out.println(it2.nextIndex()+":"+it2.next());
            }
    
            System.out.println("ListIterator反序");
            //一定要先进行由前向后输出,之后才能进行由后向前的输出,所以这里的新迭代器直接反序什么都输出不了
            ListIterator it3 = list.listIterator();
            while (it2.hasPrevious()){
                System.out.println(it2.previousIndex()+":"+it2.previous());
            }
    
            //判断
            System.out.println(list.contains("a"));//是否存在元素a
            System.out.println(list.isEmpty());//是否为空
    
            //获取元素位置
            System.out.println(list.indexOf("a"));
    
        }
        
        public class Demo2 {
        public static void main(String[] args) {
            //创建集合
            List list = new ArrayList();
            //添加数字类型数字会自动装箱
            list.add(23);
            list.add(23.23);
            list.add(2345);
            list.add(234.3453);
            list.add(342.2);
            System.out.println(""+list.size());
            System.out.println(list);
    
            //删除
    //        list.remove(1);
    //        list.remove((Object)23);
    //        list.remove(new Integer(23));
            //上面三种结果一致
    
            List list2 = list.subList(1,4);//含左不含右
            System.out.println(list2);
        }
    }
    

标签:java,list,System,add,day12,println,col,out
From: https://www.cnblogs.com/onlyxue/p/16791675.html

相关文章

  • Java将Excel转换为ODS
    前言 ODS(OpenDocumentSpreadsheet)是一种基于XML的文件格式,可以使用OpenOffice.org的Calc组件打开和建立。与MSExcel文件类似,ODS文件将数据存储在组织成行和列的单元格......
  • Java Scanner
    JavaScanner类java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。下面是创建Scanner对象的基本语法:Scanners=newScanner(System......
  • 求最大值(java可变长参数)
    publicclasstest{publicstaticvoidmain(String[]args){testt=newtest();inti=t.max(9,8,7,4,50);System.out.print(i);}......
  • java 枚举和迭代器遍历
    初始向量Vector<String>l=newVector<>();l.add("Amit");l.add("Raj");l.add("Pathak");l.add("Sumit");l.add("Aron")......
  • Java五个最常用的集合类之间的区别和联系
    Map<String,?>只能是只读模式,不能增加,因为增加的时候不知道该写入什么类型的值;Map<String,Object>可以读和写,只要是所有Object类的子类都可以。  常用的集合类有一......
  • java response 异常抛出
    原因:如果没有自定义异常,那么http请求遇到错误时就会返回系统自带的异常,不利于问题的排查{"timestamp":"2022-10-14T05:38:16.881+00:00","status":500,......
  • Java学习之路:流程控制
    2022-10-1110:58:41......
  • 打印三角形(java的for循环)
    publicclasstest{publicstaticvoidmain(String[]args){//输出n行的三角形intn=25;for(intj=0;j<n;j++){for(int......
  • java 在命令行下引用三方包编译并执行
    参考:https://blog.csdn.net/xuejiaodream/article/details/79161938方法:命令行进行.java当前目录,执行下面的命令编译java程序,$javac-cp".:fastjson-1.2.4.jar"HttpDemo......
  • JavaScript简单特效:页面背景颜色在线改变
    基于JavaScript以及canvas实现输入颜色预览以及背景颜色变为输入颜色值的效果。输入框中默认值为黑色,下方画布显示该颜色。通过输入新的颜色值后,点击【显示颜色】按钮,画......