首页 > 编程语言 >java的ArrayList类

java的ArrayList类

时间:2024-03-29 17:04:02浏览次数:24  
标签:arr java ArrayList System add println eq out

ArrayList<E>E是自定义数据类型

ArrayList类:
构造函数:

 成员方法:
 

public boolean add(E e):

将指定元素加到集合末尾

Appends the specified element to the end of this list.

public class Array {
    public static void main(String[] args) {
        ArrayList arr=new ArrayList();
        arr.add("nihao");//从末尾添加
        arr.add(67);
        arr.add("he");
        System.out.println(arr);//[nihao, 67, he]

    }
}

可以指定向里面特定数据类型

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
    }
}

public void add(int index, E element)

给集合的指定位置插入指定的元素

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
    }
}

public E get(int index)

返回索引处的元素

Returns the element at the specified position in this list.

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
    }
}

public int size()

返回集合中的集合元素的个数

Returns the number of elements in this list.

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
        int size=arr.size();
        System.out.println(size);//3
    }
}

public E remove(int index)

删除指定索引处的元素,返回被删除的元素

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
        int size=arr.size();
        System.out.println(size);//3

        String el2=arr.remove(2);
        System.out.println(el2);//my
        System.out.println(arr);// [nihao, eq]
        
    }
}

public boolean remove(Object o)

删除指定的元素,删除成功返回true,第一个出现的数据

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that Objects.equals(o, get(i)) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

public class Array {
    public static void main(String[] args) {
        ArrayList<String> arr=new ArrayList();
        arr.add("nihao");
        arr.add("eq");
        System.out.println(arr);//[nihao, eq]
        arr.add(2,"my");
        System.out.println(arr);//[nihao, eq, my]
        String el=arr.get(1);
        System.out.println(el);//eq
        int size=arr.size();
        System.out.println(size);//3

        String el2=arr.remove(2);
        System.out.println(el2);//my
        System.out.println(arr);// [nihao, eq]

        System.out.println(arr.remove("eq"));//true
        System.out.println(arr);//[nihao]

    }
}

public E set(int index, E element)

修改指定索引的值

Replaces the element at the specified position in this list with the specified element.

         arr.set(0,"hhh");
         System.out.println(arr);//[hhh]

从集合中遍历元素,删除含有特定内容的元素,应该怎么做:

方法一:每次删除一个元素,索引-1;

方法二:从集合的最后开始向前遍历,可以避免漏掉元素

标签:arr,java,ArrayList,System,add,println,eq,out
From: https://blog.csdn.net/luosuss/article/details/137055942

相关文章

  • 2.java openCV4.x 入门-hello OpenCV
    专栏简介......
  • java postgres单体库迁移postgres集群库java
    packagecom.slsl.digital.twin.manage.controller.project;importcom.google.common.collect.Lists;importcom.slsl.digital.twin.common.utils.CollectionUtils;importjava.sql.*;importjava.util.*;importjava.util.stream.Collectors;publicclassTest{......
  • Java Swing容器:文件对话框
           文件对话框是专门用于对文件或目录进行浏览和选择的对话框。可以使用JFileChooser类创建文件对话框,其主要构造方法如下:JFileChooser():根据用户默认目录创建文件对话框。JFileChooser(FilecurrentDirectory):根据File型参数所指定的目录创建文件对话框。JFileCho......
  • JavaScript快速入门笔记之七(String:字符串类型、RegExp:正则表达式)
    JavaScript快速入门笔记之七(String:字符串类型、RegExp:正则表达式)String:字符串类型什么是字符串?底层本质:一串字符组成的只读字符数组包装类型:临时封装原始类型数据,并提供对数据操作方法的对象——类型名和原始类型名相同!StringNumberBoolean何时使用:不必手动创建!......
  • Java Swing组件:表格
           表格(JTable)是将数据以二维的形式展示给用户,它包括行和列,每一行表示一个对象,例如一个学生,每一列表示对象的一种属性,例如学生的学号、姓名等。表格组件是采用MVC(ModelViewController)模式进行设计,按照MVC的设计理念,JTable类属于视图,对应的数据模型是TableModel接......
  • Java企业电子招投标系统源代码,支持二次开发,采用Spring cloud框架
    在数字化采购领域,企业需要一个高效、透明和规范的管理系统。通过采用SpringCloud、SpringBoot2、Mybatis等先进技术,我们打造了全过程数字化采购管理平台。该平台具备内外协同的能力,通过待办消息、招标公告、中标公告和信息发布等功能模块,实现了对供应商的集中管理和风险控制......
  • JAVA版鸿鹄云商B2B2C:解析多商家入驻直播带货商城系统的实现与应用
     一、技术选型java开发语言:java是一种跨平台的编程语言,适用于大型企业级应用开发。使用java开发直播商城可以保证系统的稳定性和可扩展性。springboot框架:springboot是一个快速构建spring应用的框架,简化了开发过程,减少了配置文件,提供了强大的自动化配置功能。使用springb......
  • 如何在Java中读取超过内存大小的文件
    读取文件内容,然后进行处理,在Java中我们通常利用Files类中的方法,将可以文件内容加载到内存,并流顺利地进行处理。但是,在一些场景下,我们需要处理的文件可能比我们机器所拥有的内存要大。此时,我们则需要采用另一种策略:部分读取它,并具有其他结构来仅编译所需的数据。接下来,我们就来......
  • 【附源码】JAVA计算机毕业设计在线考研刷题系统(springboot+mysql+开题+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着信息技术的飞速发展,计算机在教育领域的应用日益广泛。特别是在线教育平台,以其便捷性、灵活性和资源共享性受到了广大师生的青睐。近年来,考研热潮......
  • 【附源码】JAVA计算机毕业设计在线考试系统的设计与实现(springboot+mysql+开题+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景随着信息技术的迅猛发展和互联网的普及,传统的教育模式正面临着深刻的变革。在线考试系统作为教育信息化进程中的重要一环,正逐渐取代传统的纸质考试方......