java 集合类 深入详解_JavaSE第三十九讲:深入详解ArrayList

java.utilInterface List

[在实现类里面有两个实现类在实际开发中非常重要:ArrayList和LinkedList]

1) ArrayList[数组列表]

Class ArrayList

java.lang.Object

java.util.AbstractCollection

java.util.AbstractList

java.util.ArrayList

public class ArrayList

extends AbstractList

implements List, RandomAccess, Cloneable, Serializable

查看其构造方法:

0818b9ca8b590ca3270a3433284dd417.png

使用add()方法往集合添加东西

boolean

Appends the specified element to the end of this list.

public boolean add(E e)

Appends the specified element to the end of this list.[往list末尾追加数据]

0818b9ca8b590ca3270a3433284dd417.png

使用get(int index)方法从集合取数据

Returns the element at the specified position in this list.

public E get(int index)

Returns the element at the specified position in this list.[返回在列表中的元素的特定位置]

0818b9ca8b590ca3270a3433284dd417.png

[index返回List的索引]

arrayList.add("hello"); 第一个往List添加,则列表中其索引为0

arrayList.add("world");第二个往List添加,则列表中其索引为1

arrayList.add("welcome");第三个往List添加,则列表中其索引为2

注意get(intindex)方法中返回类型是一个对象Object,再取的时候要强制类型转化为String。[注意这边放进去的是字符串,出来的是对象,所要要进行强制类型转换]

package com.ahuier;

import java.util.ArrayList;

public class ArrayListTest {

public static void main(String[] args) {

ArrayList arrayList = new ArrayList();

arrayList.add("hello");

arrayList.add("world");

arrayList.add("welcome");

String s1 = (String)arrayList.get(0);

String s2 = (String)arrayList.get(1);

String s3 = (String)arrayList.get(2);

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

}

}

编译执行结果:

hello

world

welcome

【说明】:Object o1 = arrayList.get(0); 正常的取的返回值是Object类型,要强制类型转换为String,所以String s1 = (String)arrayList.get(0);为正确写法。

以上代码段增加一行:

String s4 = (String)arrayList.get(3);

编译运行:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

at java.util.ArrayList.rangeCheck(Unknown Source)

at java.util.ArrayList.get(Unknown Source)

at com.ahuier.ArrayListTest.main(ArrayListTest.java:18)

【说明】:发生List越界异常,因为没有取的数据不存在。

以上代码段增加一行:

ArrayList arrayList = new ArrayList();

arrayList.add("hello");

arrayList.add("world");

arrayList.add("welcome");

arrayList.add("welcome");

Object o1 = arrayList.get(0);

String s1 = (String)arrayList.get(0);

String s2 = (String)arrayList.get(1);

String s3 = (String)arrayList.get(2);

String s4 = (String)arrayList.get(3);

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

System.out.println(s4);

编译运行:

hello

world

welcome

welcome

【说明】:List可以往里面添加相同的数据。

继续查看JDK Doc中的方法:

public int size()

Returns the number of elements in this list.

[List列表中的长度,类似数组中的length]

for(int i = 0; i < arrayList.size(); i++){

System.out.println(arrayList.get(i));

}

【说明】:遍历整个集合,打印出其中的值

public void clear()

Removes all of the elements from this list. The list will be empty after this call returns.

[清空集合中的数据]

arrayList.clear();

【说明】:清空整个集合

public boolean isEmpty()

Returns true if this list contains no elements.[判断列表中是否包含元素]

System.out.println(arrayList.isEmpty());

【说明】:判断列表是否没有内容。

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).[根据索引,清空List中的元素]

arrayList.remove(0);

【说明】:清空索引为0的列表中的元素,注意这边清空了列表中索引为0的元素后,其他元素的索引也相应的往前移动。

public boolean remove(Object o)

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 (o==null ? get(i)==null : o.equals(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).

[删除具体的对象]

arrayList.remove("welcome");

【说明】:删除了List中的welcome元素。这里面还有很多学问。

public int indexOf(Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

[返回特定元素的索引,如果没有这个元素,则返回-1]

System.out.println(arrayList.indexOf("hello"));

【说明】:返回hello所在列表的索引

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值