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 ...
随机推荐
- 中国区 Azure 服务和定价模式概述
由世纪互联运营的 Microsoft Azure 是第一个在中国正式商用,符合中国政府相关法规要求的国际化公有云服务.本文剖析了由世纪互联运营的 Microsoft Azure 的运营模式.采购模式. ...
- pc端常见布局---水平居中布局 单元素不定宽度
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- momentum公式
momentum对于w的更新公式: http://caffe.berkeleyvision.org/tutorial/solver.html
- JSONPath - XPath for JSON
http://goessner.net/articles/JsonPath/ [edit] [comment] [remove] |2007-02-21| e1 # JSONPath - XPath ...
- MFC:AfxSetResourceHandle
AfxGetResourceHandle用于获取当前资源模块句柄. 而AfxSetResourceHandle则用于设置程序目前要使用的资源模块句柄,一般在InitInstance()里调用.
- openstack keystone修改horizon密码
命令行修改密码:token在/etc/keystone/keystone.conf中的一个admin_token export OS_SERVICE_TOKEN=165a1766c12a497b8fb ...
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
eclipse 中main()函数中的String[] args如何使用? 右击你的项目,选择run as中选择 run configuration,选择arguments总的program argu ...
- 功能强大的CURL
linux下的curl,有着非同一般的魔力,有人称它为下载工具,我更倾向于叫它“文件传输工具”因为它好像无所不能.从常见的 FTP, HTTP, TELNET, 等协议,还支持代理服务器,cook ...
- ubuntu系统普通用户密码忘记之重置
当我们在使用ubuntu系统忘记普通用户登录密码的时候,会被系统在登录界面拒之门外而不得入,这时候只好需要我们去重新设置密码,具体做法如下: 系统重启,在GRUB模式下选择Advanced Optio ...
- Ubuntu安装sogou拼音输入法
1.更新系统:sudo apt-get update 2.更新相关依赖 sudo apt-get install fcitx -f 2.安装fcitx:sudo apt-get install fci ...