ArrayList常用方法

步骤 1 : 增加

add 有两种用法:

第一种是直接add对象,把对象加在最后面

heros.add(new Hero("hero " + i));

第二种是在指定位置加对象

heros.add(3, specialHero);

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 把5个对象加入到ArrayList中
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
System.out.println(heros); // 在指定位置增加对象
Hero specialHero = new Hero("special hero");
heros.add(3, specialHero); System.out.println(heros.toString()); } }
package charactor; public class Hero {
public String name;
public float hp; public int damage; public Hero() { } // 增加一个初始化name的构造方法
public Hero(String name) { this.name = name;
} // 重写toString方法
public String toString() {
return name;
} }

步骤 2 : 判断是否存在

通过方法contains 判断一个对象是否在容器中

判断标准: 是否是同一个对象,而不是name是否相同

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero); System.out.println(heros);
// 判断一个对象是否在容器中
// 判断标准: 是否是同一个对象,而不是name是否相同
System.out.print("虽然一个新的对象名字也叫 hero 1,但是contains的返回是:");
System.out.println(heros.contains(new Hero("hero 1")));
System.out.print("而对specialHero的判断,contains的返回是:");
System.out.println(heros.contains(specialHero));
} }

步骤 3 : 获取指定位置的对象

通过get获取指定位置的对象,如果输入的下标越界,一样会报错

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero); //获取指定位置的对象
System.out.println(heros.get(5));
//如果超出了范围,依然会报错
System.out.println(heros.get(6)); } }

步骤 4 : 获取对象所处的位置

indexOf用于判断一个对象在ArrayList中所处的位置

与contains一样,判断标准是对象是否相同,而非对象的name值是否相等

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero); System.out.println(heros);
System.out.println("specialHero所处的位置:"+heros.indexOf(specialHero));
System.out.println("新的英雄,但是名字是\"hero 1\"所处的位置:"+heros.indexOf(new Hero("hero 1"))); }
}

步骤 5 : 删除

remove用于把对象从ArrayList中删除

remove可以根据下标删除ArrayList的元素

heros.remove(2);

也可以根据对象删除

heros.remove(specialHero);

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero); System.out.println(heros);
heros.remove(2);
System.out.println("删除下标是2的对象");
System.out.println(heros);
System.out.println("删除special hero");
heros.remove(specialHero);
System.out.println(heros); }
}

步骤 6 : 替换

set用于替换指定位置的元素

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero); System.out.println(heros);
System.out.println("把下标是5的元素,替换为\"hero 5\"");
heros.set(5, new Hero("hero 5"));
System.out.println(heros);
}
}

步骤 7 : 获取大小

size 用于获取ArrayList的大小

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero);
System.out.println(heros);
System.out.println("获取ArrayList的大小:");
System.out.println(heros.size());
}
}

步骤 8 : 转换为数组

toArray可以把一个ArrayList对象转换为数组。

需要注意的是,如果要转换为一个Hero数组,那么需要传递一个Hero数组类型的对象给toArray(),这样toArray方法才知道,你希望转换为哪种类型的数组,否则只能转换为Object数组

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
}
Hero specialHero = new Hero("special hero");
heros.add(specialHero);
System.out.println(heros);
Hero hs[] = (Hero[])heros.toArray(new Hero[]{});
System.out.println("数组:" +hs); }
}

步骤 9 : 把另一个容器所有对象都加进来

addAll 把另一个容器所有对象都加进来

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
} System.out.println("ArrayList heros:\t" + heros); //把另一个容器里所有的元素,都加入到该容器里来
ArrayList anotherHeros = new ArrayList();
anotherHeros.add(new Hero("hero a"));
anotherHeros.add(new Hero("hero b"));
anotherHeros.add(new Hero("hero c"));
System.out.println("anotherHeros heros:\t" + anotherHeros);
heros.addAll(anotherHeros);
System.out.println("把另一个ArrayList的元素都加入到当前ArrayList:");
System.out.println("ArrayList heros:\t" + heros); }
}

步骤 10 : 清空

clear 清空一个ArrayList

package collection;

import java.util.ArrayList;

import charactor.Hero;

public class TestCollection {
public static void main(String[] args) {
ArrayList heros = new ArrayList(); // 初始化5个对象
for (int i = 0; i < 5; i++) {
heros.add(new Hero("hero " + i));
} System.out.println("ArrayList heros:\t" + heros);
System.out.println("使用clear清空");
heros.clear();
System.out.println("ArrayList heros:\t" + heros); }
}

Java自学-集合框架 ArrayList常用方法的更多相关文章

  1. Java自学-集合框架 ArrayList和LinkedList的区别

    ArrayList和LinkedList的区别 步骤 1 : ArrayList和LinkedList的区别 ArrayList ,插入,删除数据慢 LinkedList, 插入,删除数据快 Arra ...

  2. Java自学-集合框架 ArrayList和HashSet的区别

    Java ArrayList和HashSet的区别 示例 1 : 是否有顺序 ArrayList: 有顺序 HashSet: 无顺序 HashSet的具体顺序,既不是按照插入顺序,也不是按照hashc ...

  3. Java自学-集合框架 LinkedList

    Java集合框架 LinkedList 序列分先进先出FIFO,先进后出FILO FIFO在Java中又叫Queue 队列 FILO在Java中又叫Stack 栈 示例 1 : LinkedList ...

  4. Java自学-集合框架 Collections

    Java集合框架 工具类Collections Collections是一个类,容器的工具类,就如同Arrays是数组的工具类 步骤 1 : 反转 reverse 使List中的数据发生翻转 pack ...

  5. Java自学-集合框架 二叉树

    Java集合框架 二叉树 示例 1 : 二叉树概念 二叉树由各种节点组成 二叉树特点: 每个节点都可以有左子节点,右子节点 每一个节点都有一个值 package collection; public ...

  6. Java自学-集合框架 与数组的区别

    Java集合框架与数组的区别 示例 1 : 使用数组的局限性 如果要存放多个对象,可以使用数组,但是数组有局限性 比如 声明长度是10的数组 不用的数组就浪费了 超过10的个数,又放不下 //Test ...

  7. Java基础-集合框架-ArrayList源码分析

    一.JDK中ArrayList是如何实现的 1.先看下ArrayList从上而下的层次图: 说明: 从图中可以看出,ArrayList只是最下层的实现类,集合的规则和扩展都是AbstractList. ...

  8. Java自学-集合框架 Collection

    Java集合框架 Collection Collection是一个接口 步骤 1 : Collection Collection是 Set List Queue和 Deque的接口 Queue: 先进 ...

  9. Java自学-集合框架 HashSet

    Java集合框架 HashSet 示例 1 : 元素不能重复 Set中的元素,不能重复 package collection; import java.util.HashSet; public cla ...

随机推荐

  1. 挑战常规 -- 为什么不要再用cookie作为储存?

    不要使用cookie当存储 Cookie 是什么? Cookie 由浏览器储存在本地,每次访问目标网址会带上的请求头,服务器可以通过Set-Cookie响应头设置Cookie. Cookie的用途 由 ...

  2. MySQL基础之数据管理【2】

    where条件筛选记录 select id,username,age from uesr where id=5; alter table user add userDesc varchar(100); ...

  3. 结对编程(Java实现)

    一.Github项目地址:https://github.com/qiannai/CreateArithmetic 二.PSP2.1表格: PSP2.1 Personal Software Proces ...

  4. win10安装ubuntu系统出现的一些问题以及解决方案

    前言 在win10系统进行安装新的ubuntu环境的时候遇到的一些问题,以及解决方案,供以后参考. 准备 从ubuntu官网下载最近版本的ubuntu系统,Ubuntu最新版本下载地址 操作系统:wi ...

  5. Linux(二)-- Linux配置及指令

    一.linux中常用软件的安装 yum install -y bash-completion vim lrzsz wget expect net-tools nc nmap tree dos2unix ...

  6. Incorrect datetime value: '' for column 'examDate' at row 1

    出问题的程序:user.setCreateTime(new Date()); 控制台图片一张,问题是:Incorrect datetime value: '' for column 'createTi ...

  7. scrapy 爬取图片

    scrapy 爬取图片 1.scrapy 有下载图片的自带接口,不用我们在去实现 setting.py设置 # 保存log信息的文件名 LOG_LEVEL = "INFO" # L ...

  8. C++ class内的 ++ 重载,左++,右++,重载示例。

    #include <iostream> // overloading "operator ++ " inside class // ++ 是一元操作符 //////// ...

  9. K-消亡的质数-(简单数学)

    https://ac.nowcoder.com/acm/contest/3346/K 题意:判断一个素数p是不是某两个数的立方差. 刚看到这道题一时半会都没有什么思路,看了题解恍然大悟,太久没碰数学或 ...

  10. 安装Rtools

    1.好多工具需要安装Rtools install.packages("installr") install.packages("stringr") ###依赖包 ...