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的更多相关文章

  1. Set容器——HashSet及常用API

    Set容器特点: ①   Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序; ②   最常用的两个Set接口的实 ...

  2. Map容器——TreeMap及常用API,Comparator和Comparable接口

    TreeMap及常用API ①   TreeMap类通过使用红黑树实现Map接口; ②   TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③   不像散列(HashMap), ...

  3. List容器——LinkedList及常用API,实现栈和队列

    LinkedList及常用API ①   LinkedList----链表 ②   LinkedList类扩展AbstractSequentialList并实现List接口 ③   LinkedLis ...

  4. Set容器——TreeSet及常用API

    TreeSet及常用Api ①   TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和检索很快; ②   在存储了大量的需要进行快速检索的排序信息的情况下,TreeSe ...

  5. Map容器——HashMap及常用API,及put,get方法解析,哈希码的产生和使用

    Map接口 ①   映射(map)是一个存储键/值对的对象.给定一个键,可以查询到它的值,键和值都是对象; ②   键必须是唯一的,值可以重复; ③   有些映射可以接收null键和null值,而有的 ...

  6. Java常用API(ArrayList类)

    Java常用API(ArrayList类) 我们为什么要使用ArrayList类? 为了更加方便的储存对象,因为使用普通的数组来存储对象太过麻烦了,因为数组的一个很大的弱点就是长度从一开始就固定了,所 ...

  7. JAVA基础学习-集合三-Map、HashMap,TreeMap与常用API

    森林森 一份耕耘,一份收获 博客园 首页 新随笔 联系 管理 订阅 随笔- 397  文章- 0  评论- 78  JAVA基础学习day16--集合三-Map.HashMap,TreeMap与常用A ...

  8. 19 常用API

    API 什么是API? API (Application Programming Interface) :应用程序编程接口 简单来说:就是Java帮我们已经写好的一些方法,我们直接拿过来用就可以了 1 ...

  9. Java | 个人总结的Java常用API手册汇总

    目录 常用API JavaAPI 1 java.lang String StringBuilder Integer parseXxx Math Object System Throwable Thre ...

随机推荐

  1. sql优化实战:从1353秒到135秒(删除索引+修改数据+重建索引)

    最近在优化日结存储过程,日结存储过程中大概包含了20多个存储过程. 发现其有一个存储过程代码有问题,进一步发现结存的数据中有一个 日期字段business_date 是有问题的,这个字段对应的类型是v ...

  2. 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  3. 【Python图像特征的音乐序列生成】如何生成旋律(大纲),以及整个项目的全部流程

    今天连看三篇论文,不是很细致地看,也没有具体去实现,只是大概明白了一些新思路.这三篇论文,一篇概述了Decoder-Encoder模型,一篇延伸这个模型,首次提出了Attention机制,最后一篇详细 ...

  4. External Pricing in C4C and ERP

    从下图可以看出,C4C的Opportunity,Sales Quote和Sales Order这些business transaction没有自己的pricing engine,使用的是在ERP Pr ...

  5. HDU 4284 Travel (Folyd预处理+dfs暴搜)

    题意:给你一些N个点,M条边,走每条边要花费金钱,然后给出其中必须访问的点,在这些点可以打工,但是需要先拿到证书,只可以打一次,也可以选择不打工之直接经过它.一个人从1号点出发,给出初始金钱,问你能不 ...

  6. 字符编码:BSTR

    typedef wchar_t WCHAR; typedef WCHAR OLECHAR; typedef OLECHAR __RPC_FAR *BSTR;;

  7. 从 Objective-C 里的 Alloc 和 AllocWithZone 谈起

    一.问题起源 一切起源于Apple官方文档里面关于单例(Singleton)的示范代码:Creating a Singleton Instance.主要的争议集中在下面这一段: static MyGi ...

  8. Hexo + Github Pages搭建的个人博客

    这个不算是新手的搭建流程,如果你恰巧看见这篇文章,希望你已经安装好node.git等软件,因为第一步的环境搭建准备并没有详写,默认都会了.希望能解决你的问题. 步骤: 一. 搭建环境准备 二.安装he ...

  9. MySQL在windows上的安装步骤

    参考文章MySQL安装及建议:https://zhuanlan.zhihu.com/p/44977117 但在进入mysql中修改root命令时,使用文章中的命令: ALTER USER 'root' ...

  10. Django之用户认证

    用户认证组件简介 功能:用session记录登录验证状态 前提:必须使用django自带的auth_user表.那这里有的同学就会有疑问了,自己不能创建自己的用户表吗? 当然可以,用户认证组件虽然只针 ...