java的ArrayList类

news2024/9/20 16:26:16

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;

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1546565.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

视觉轮速滤波融合1讲:理论推导

视觉轮速滤波融合理论推导 文章目录 视觉轮速滤波融合理论推导1 坐标系2 轮速计2.1 运动学模型2.2 外参 3 状态和协方差矩阵3.1 状态3.2 协方差矩阵 4 Wheel Propagation4.1 连续运动学4.2 离散积分4.2.1 状态均值递推4.2.2 协方差递推 5 Visual update5.1 视觉残差与雅可比5.2…

P1135 奇怪的电梯 (双向bfs)

输入输出样例 输入 5 1 5 3 3 1 2 5输出 3说明/提示 对于 100%100% 的数据&#xff0c;1≤N≤200&#xff0c;1≤A,B≤N&#xff0c;0≤Ki​≤N。 本题共 1616 个测试点&#xff0c;前 1515 个每个测试点 66 分&#xff0c;最后一个测试点 10 分。 重写AC代码&#xff1…

C++_回文串

目录 回文子串 最长回文子串 分割回文串 IV 分割回文串 II 最长回文子序列 让字符串成为回文串的最少插入次数 回文子串 647. 回文子串 思路&#xff0c;i j表示改范围内是否为回文串&#xff0c; ②倒着遍历是为了取出dp[i 1][j - 1] ③i j 只有一对&#xff0c;不会重复…

每天上万简历,录取不到1%!阿里腾讯的 offer 都给了哪些人?

三月天杨柳醉春烟~正是求职好时节~ 与去年秋招的冷淡不同&#xff0c;今年春招市场放宽了许多&#xff0c;不少企业纷纷抛出橄榄枝&#xff0c;各大厂的只差把“缺人”两个字写在脸上了。 字节跳动技术方向开放数10个类型岗位&#xff0c;研发需求占比60%&#xff0c;非研发新增…

【数据结构】双向奔赴的爱恋 --- 双向链表

关注小庄 顿顿解馋๑ᵒᯅᵒ๑ 引言&#xff1a;上回我们讲解了单链表(单向不循环不带头链表)&#xff0c;我们可以发现他是存在一定缺陷的&#xff0c;比如尾删的时候需要遍历一遍链表&#xff0c;这会大大降低我们的性能&#xff0c;再比如对于链表中的一个结点我们是无法直接…

C/C++ 语言中的 ​if...else if...else 语句

C/C 语言中的 ​if...else if...else 语句 1. if statement2. if...else statement3. if...else if...else statementReferences 1. if statement The syntax of the if statement is: if (condition) {// body of if statement }The code inside { } is the body of the if …

《剑指 Offer》专项突破版 - 面试题 93 : 最长斐波那契数列(C++ 实现)

题目链接&#xff1a;最长斐波那契数列 题目&#xff1a; 输入一个没有重复数字的单调递增的数组&#xff0c;数组中至少有 3 个数字&#xff0c;请问数组中最长的斐波那契数列的长度是多少&#xff1f;例如&#xff0c;如果输入的数组是 [1, 2, 3, 4, 5, 6, 7, 8]&#xff0…

代码随想录训练营Day33:● 1005.K次取反后最大化的数组和 ● 134. 加油站 ● 135. 分发糖果

1005.K次取反后最大化的数组和 题目链接 https://leetcode.cn/problems/maximize-sum-of-array-after-k-negations/ 题目描述 思路 1、自己的想法 class Solution {public int largestSumAfterKNegations(int[] nums, int k) {sorted(nums);//遇到 <0 的 &#xff0c;就…

CTF题型 nodejs(1) 命令执行绕过典型例题

CTF题型 nodejs(1) 命令执行绕过 文章目录 CTF题型 nodejs(1) 命令执行绕过一.nodejs中的命令执行二.nodejs中的命令绕过1.编码绕过2.拼接绕过3.模板字符串4.Obejct.keys5.反射6.过滤中括号的情况典型例题1.[GFCTF 2021]ez_calc2.[西湖论剑 2022]Node Magical Login 一.nodejs中…

黑马头条day5总结

1、surefire-reports for the individual test results. 借鉴&#xff1a;【已解决】surefire-reports for the individual test results.-CSDN博客 Please refer to D:\javashizhan01\heima-leadnews\heima-leadnews-service\heima-leadnews-article\target\surefire-report…

手撕算法-数组中的第K个最大元素

描述 分析 使用小根堆&#xff0c;堆元素控制在k个&#xff0c;遍历数组构建堆&#xff0c;最后堆顶就是第K个最大的元素。 代码 class Solution {public int findKthLargest(int[] nums, int k) {// 小根堆PriorityQueue<Integer> queue new PriorityQueue<>…

k8s入门到实战(六)—— ConfigMap介绍

ConfigMap configmap 是 k8s 中的资源对象&#xff0c;用于保存非机密性的配置的&#xff0c;数据可以用 kv 键值对的形式保存&#xff0c;也可通过文件的形式保存。 什么是 configmap 在 k8s 中&#xff0c;ConfigMap 是一种用于存储应用程序配置数据的对象。它允许将配置信…

【python】Jupyter Notebook 修改默认路径

文章目录 一、修改前&#xff08;一&#xff09;问题&#xff08;二&#xff09;修改前的默认路径 二、修改配置文件、更改路径&#xff08;一&#xff09;找到配置文件并打开&#xff08;二&#xff09;创建目标文件夹、得到新的路径&#xff08;三&#xff09;修改配置文件 三…

小目标检测篇 | YOLOv8改进之GSConv + Slim Neck提升小目标检测效果

前言:Hello大家好,我是小哥谈。在文章中,作者提出了一种新方法GSConv来减轻模型的复杂度并保持准确性。GSConv可以更好地平衡模型的准确性和速度。并且,提供了一种设计范式Slim Neck,以实现检测器更高的计算成本效益。实验过程中,与原始网络相比,改进方法获得了最优秀的…

nginx--解决响应头带Set-Cookie导致的验证失败

解决响应头带Set-Cookie导致的验证失败 前言给nginx.conf 设置Secure配置完成后会发现cookie就不会发生变化了 前言 在用nginx做代理的时候&#xff0c;会发现nginx在访问不同ip请求的时候会带setCookie 导致后端就是放开cookie验证&#xff0c;在访问玩这个链接他更新了cooki…

成都爱尔胡建斌院长强调黄斑病变是眼睛哪儿出了问题

黄斑位于眼球内部的眼底的视网膜区域&#xff0c;处于人眼的光学中心区&#xff0c;是视力轴线的投影点。它是人眼视网膜中央视觉细胞最集中的部位。黄斑中心多为锥形细胞&#xff0c;对明暗不敏感&#xff0c;对色敏感。黄斑外围多为柱形细胞&#xff0c;对明暗敏感,对色几乎不…

javaWeb私人牙科诊所管理系统

一、摘要 随着科技的飞速发展&#xff0c;计算机已经广泛的应用于各个领域之中。在医学领域中&#xff0c;计算机主要应用于两个方面&#xff1a;一是医疗设备智能化&#xff0c;以硬件为主。另一种是病例信息管理系统&#xff08;HIS&#xff09;以软件建设为主&#xff0c;以…

深度学习pytorch——减少过拟合的几种方法(持续更新)

1、增加数据集 2、正则化(Regularization) 正则化&#xff1a;得到一个更加简单的模型的方法。 以一个多项式为例&#xff1a; 随着最高次的增加&#xff0c;会得到一个更加复杂模型&#xff0c;模型越复杂就会更好的拟合输入数据的模型&#xff08;图-1&#xff09;&#…

【数据结构与算法】用染色法判定二分图

问题描述 给定一个 n 个点 m 条边的无向图&#xff0c;图中可能存在重边和自环。 请你判断这个图是否是二分图。 输入格式 第一行包含两个整数 n 和 m。 接下来 m 行&#xff0c;每行包含两个整数 u 和 v&#xff0c;表示点 u 和点 v 之间存在一条边。 输出格式 如果给定图…

js中如何用点击地图获取经纬度

要实现在地图上点击并获取被点击地址的经纬度&#xff0c;然后渲染至页面中的功能&#xff0c;你需要首先确保你使用的地图API支持点击事件&#xff0c;并且能够返回点击位置的经纬度。以高德地图&#xff08;AMap&#xff09;为例&#xff0c;你可以按照以下步骤实现这个功能&…