List容器——ArrayList及常用API
List:
① List容器是有序的collection(也称为序列)。此接口的用户可以对List容器中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。List容器允许插入重复的值,包括null;
② 最常见的两个List接口的实现类是ArrayList和LinkedList;
ArrayList及常用API:
① ArrayList—动态数组;
② ArrayList类扩展了AbstractList并实现了List接口;
③ 支持可随需增长的动态数组;
④ ArrayList构造方法:
a) ArrayList()
b) ArrayList(Collection c)
c) ArrayList(int capacity)
⑤ 除了继承的方法外,ArrayList常用方法:
a) E get(int index) 返回此列表中指定位置上的元素
b) int indexOf(Object o) 返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。
c) ……
ArrayList新增,修改,输出
List<String> nList = new ArrayList<String>();
nList.add("zhangsan");// 将指定的元素添加到此列表的尾部
nList.add("lisi");
nList.add("wangwu");
nList.add(1, "jay");// 将指定的元素插入此列表中的指定位置
nList.set(0, "Ali");// 用指定的元素替代此列表中指定位置上的元素
System.out.println("使用迭代器对象来进行统一的遍历");
Iterator<String> it = nList.iterator();
while (it.hasNext()) {
String name = it.next();
System.out.println(name);
} System.out.println("使用增强for循环来进行统一的遍历");
for(String name:nList){
System.out.println(name);
}
输出结果为:
使用迭代器对象来进行统一的遍历
Ali
jay
lisi
wangwu
使用增强for循环来进行统一的遍历
Ali
jay
lisi
wangwu
接上面测试常用方法:
System.out.println("****************************************");
System.out.println(nList.indexOf("lisi"));//返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。
System.out.println(nList.remove("lisi"));//移除此列表中首次出现的指定元素(如果存在)。
System.out.println(nList.remove(0));//移除此列表中指定位置上的元素
System.out.println(nList.size());//返回此列表中的元素数。--原本有4个,上面删除了2个。结果为2
System.out.println(nList.contains("zhangsan"));//如果此列表中包含指定的元素,则返回 true。
System.out.println(nList.get(0));//返回此列表中指定位置上的元素。
System.out.println(nList.isEmpty());//如果此列表中没有元素,则返回 true
nList.clear();//移除此列表中的所有元素
System.out.println(nList.isEmpty());
输出结果:
****************************************
2
true
Ali
2
false
jay
false
true
新建一个类,添加
class Student{
private String name;
private int age;
}
为其添加get,set方法

将会自动创建get,set方法
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;
}
为其创建带2个参数的构造方法

public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
在主方法中添加元素及操作
List<Student> stuList=new ArrayList<Student>();
Student stu1=new Student("zhangsan", 10);
Student stu2=new Student("lisi", 20);
Student stu3=new Student("wangwu", 30);
Student stu4=new Student("zhaoliu", 25);
Student stu5=new Student("tianqi", 15);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
stuList.add(stu5);
Student stu6=new Student("tianqi", 15);
System.out.println(stuList.indexOf(stu6));//-1
但我们想要返回当名字与年龄相同时就返回索引;
查看ArrayList中的indexOf方法如下:(查看方法ctrl+鼠标左键选中ArrayList,再在大纲视图中找到indexOf(Object)方法):
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
此处用的equals比较,说明student每创建一个都不可能会相同,所以我们要重构Student类中的equals方法;
在Eclipse中也提供了重构equals的方法:

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
修改好Student类之后再进行测试:
System.out.println(stuList.indexOf(stu6));//-1
System.out.println(stuList.contains(stu6));
System.out.println(stuList.remove(stu6));//remove中用equals方法删除,所有会将stu5一并删除
System.out.println(stuList.indexOf(stu5));
System.out.println(stuList.size());
未重构equals()之前,输出结果为:
-1
false
false
4
5
重构了equals ()之后,再输出:
输出结果为:
4
true
true
-1
4
List容器——ArrayList及常用API的更多相关文章
- Set容器——HashSet及常用API
Set容器特点: ① Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序; ② 最常用的两个Set接口的实 ...
- Map容器——TreeMap及常用API,Comparator和Comparable接口
TreeMap及常用API ① TreeMap类通过使用红黑树实现Map接口; ② TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③ 不像散列(HashMap), ...
- List容器——LinkedList及常用API,实现栈和队列
LinkedList及常用API ① LinkedList----链表 ② LinkedList类扩展AbstractSequentialList并实现List接口 ③ LinkedLis ...
- Set容器——TreeSet及常用API
TreeSet及常用Api ① TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和检索很快; ② 在存储了大量的需要进行快速检索的排序信息的情况下,TreeSe ...
- Map容器——HashMap及常用API,及put,get方法解析,哈希码的产生和使用
Map接口 ① 映射(map)是一个存储键/值对的对象.给定一个键,可以查询到它的值,键和值都是对象; ② 键必须是唯一的,值可以重复; ③ 有些映射可以接收null键和null值,而有的 ...
- Java常用API(ArrayList类)
Java常用API(ArrayList类) 我们为什么要使用ArrayList类? 为了更加方便的储存对象,因为使用普通的数组来存储对象太过麻烦了,因为数组的一个很大的弱点就是长度从一开始就固定了,所 ...
- JAVA基础学习-集合三-Map、HashMap,TreeMap与常用API
森林森 一份耕耘,一份收获 博客园 首页 新随笔 联系 管理 订阅 随笔- 397 文章- 0 评论- 78 JAVA基础学习day16--集合三-Map.HashMap,TreeMap与常用A ...
- 19 常用API
API 什么是API? API (Application Programming Interface) :应用程序编程接口 简单来说:就是Java帮我们已经写好的一些方法,我们直接拿过来用就可以了 1 ...
- Java | 个人总结的Java常用API手册汇总
目录 常用API JavaAPI 1 java.lang String StringBuilder Integer parseXxx Math Object System Throwable Thre ...
随机推荐
- 【虚拟机-部署】通过 Powershell 来调整 ARM 模式下虚拟机的尺寸
需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...
- SharePoint运行状况分析器有关磁盘空间不足的警告
对于负责管理SharePoint内部部署安装的SharePoint管理员,SharePoint Health Analyzer是一款出色的工具.此功能不仅有助于解决服务器故障和服务失败的问题,还提供了 ...
- SVN合并步骤
1.trunk->branch/tag 分支路径在分支文件夹中,选择右键检出 2.合并分支到主干分支新增 1.txt 文件 需要合并到主干 在trunck->鼠标右键合并->合并到不 ...
- Nginx 基本配置介绍
一.什么是Nginx Nginx 是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器. Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻 ...
- 洛谷 P1433 吃奶酪
题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...
- python基础教程总结15——5 虚拟茶话会
聊天服务器: 服务器能接受来自不同用户的多个连接: 允许用户同时(并行)操作: 能解释命令,例如,say或者logout: 容易拓展 套接字和端口: 套接字是一种使用标准UNIX文件描述符(file ...
- Android(java)学习笔记131:关于构造代码块,构造函数的一道面试题(华为面试题)
1. 代码实例: package text; public class TestStaticCon { public static int a = 0; static { a = 10; System ...
- macbook secureCRT终端中文乱码的问题
最近mac用crt中文总是显示的是一串串问号, 而用自带的终端软件就不会出现乱码, 经过一番折腾暂时解决了这一问题, 方法如下: 1. 打开终端操作 sudo vim /etc/profile 在最后 ...
- 【转载】Alpha、Beta、RC、GA版本的区别
转自:http://www.blogjava.net/RomulusW/archive/2008/05/04/197985.html Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只 ...
- Bootstrap 徽章(Badges)
本章将讲解Bootstrap徽章(Badges),徽章与标签相似,主要的区别是徽章的圆角比较圆滑. 徽章(Badges)主要用于突出显示新的或未读的项,如果使用徽章,只需要把<span clas ...