建设网站好公司爱站网关键词密度
Java 集合:ArrayList
什么是ArrayList
Java.util.ArrayList类是一个动态数组类型,也就是说,ArrayList对象既有数组的特征,也有链表的特征。可以随时从链表中添加或删除一个元素。ArrayList实现了List接口。
数组是静态的,数组被初始化之后,数组长度就不能再改变了。ArrayList是可以动态改变大小的。那么,什么时候使用Array(数组),什么时候使用ArrayList?答案是:当我们不知道到底有多少个数据元素的时候,就可使用ArrayList;如果知道数据集合有固定的多少个元素,就用数组。
一、ArrayList构造函数
ArrayList类支持三个构造方法。
Arraylist()
这个构造方法构造了一个空的链表。
ArrayList(Collection<? extends E> c)
这个构造方法构造了一个包含指定元素集合的链表,注意,这里的字符E是一个标记,用来表示集合中元素的类型。至于具体是什么类型,需要你在使用这个构造方法的时候来指定。
ArrayList(int initialCapacity)
这是第三个构造方法,构造了一个指定大小但内容为空的链表。initialCapacity参数就是初始容量大小。
- 举例来说,如果你要创建一个空的数组链表,用来存放String类型的对象,那么你可以像下面这样做:
ArrayList<String> list = new ArrayList<String>();
- 如果你需要创建一个指定初始容量的数组链表,你可以像下面这样做:
ArrayList<Integer> list = new ArrayList<Integer>(66);
注意:ArrayList类只支持对象类型,不支持 基础数据类型。就是说ArrayList对象只能存放对象,不能存放基础数据类型的数据。
二、ArrayList常用方法
下面是总结了一些比较常用的ArrayList类成员方法:
-
增加元素到链表中
- boolean add(Element e)
增加指定元素到链表尾部. - void add(int index, Element e)
增加指定元素到链表指定位置.
- boolean add(Element e)
-
从链表中删除元素
- void clear()
从链表中删除所有元素. - E remove(int index)
删除链表中指定位置的元素. - protected void removeRange(int start, int end)
删除链表中从某一个位置开始到某一个位置结束的元素。
- void clear()
-
获取链表中的元素
- E get(int index)
获取链表中指定位置处的元素. - Object[] toArray()
获取一个数组,数组中所有元素是链表中的元素.(即将链表转换为一个数组)
- E get(int index)
-
修改某个元素
- E set(int index, E element)
将链表中指定位置上的元素替换成新元素。
- E set(int index, E element)
-
搜索元素
- boolean contains(Object o)
如果链表包含指定元素,返回true. - int indexOf(Object o)
返回元素在链表中第一次出现的位置,如果返回-1,表示链表中没有这个元素。 - int lastIndexOf(Object o)
返回元素在链表中最后一次出现的位置,如果返回-1,表示链表中没有这个元素。
- boolean contains(Object o)
-
检查链表是否为空
- boolean isEmpty()
返回true表示链表中没有任何元素.
- boolean isEmpty()
-
获取链表大小
- int size()
返回链表长度(链表包含元素的个数).
- int size()
-
以上是ArrayList类中使用比较多的成员方法。 详情参考java官方api
三、ArrayList使用实例
package com.cyj;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;public class ArrayListExamples {public static void main(String[] args) {// 创建一个空的数组链表对象list,list用来存放String类型的数据ArrayList<String> list = new ArrayList<String>();// 增加元素到list对象中list.add("Item1");list.add("Item2");list.add(2, "Item3"); // 此条语句将会把“Item3”字符串增加到list的第3个位置。list.add("Item4");// 显示数组链表中的内容System.out.println("The arraylist contains the following elements: " + list);// 检查元素的位置int pos = list.indexOf("Item2");System.out.println("The index of Item2 is: " + pos);// 检查数组链表是否为空boolean check = list.isEmpty();System.out.println("Checking if the arraylist is empty: " + check);// 获取链表的大小int size = list.size();System.out.println("The size of the list is: " + size);// 检查数组链表中是否包含某元素boolean element = list.contains("Item5");System.out.println("Checking if the arraylist contains the object Item5: " + element);// 获取指定位置上的元素String item = list.get(0);System.out.println("The item is the index 0 is: " + item);// 遍历arraylist中的元素// 第1种方法: 循环使用元素的索引和链表的大小System.out.println("Retrieving items with loop using index and size list");for (int i = 0; i < list.size(); i++) {System.out.println("Index: " + i + " - Item: " + list.get(i));}// 第2种方法:使用foreach循环System.out.println("Retrieving items using foreach loop");for (String str : list) {System.out.println("Item is: " + str);}// 第三种方法:使用迭代器// hasNext(): 返回true表示链表链表中还有元素// next(): 返回下一个元素System.out.println("Retrieving items using iterator");for (Iterator<String> it = list.iterator(); it.hasNext();) {System.out.println("Item is: " + it.next());}// 替换元素list.set(1, "NewItem");System.out.println("The arraylist after the replacement is: " + list);// 移除元素// 移除第0个位置上的元素list.remove(0);// 移除第一次找到的 "Item3"元素list.remove("Item3");System.out.println("The final contents of the arraylist are: " + list);// 转换 ArrayList 为 ArrayString[] simpleArray = list.toArray(new String[list.size()]);System.out.println("The array created after the conversion of our arraylist is: " + Arrays.toString(simpleArray));}}
结果如下:
The arraylist contains the following elements: [Item1, Item2, Item3, Item4]
The index of Item2 is: 1
Checking if the arraylist is empty: false
The size of the list is: 4
Checking if the arraylist contains the object Item5: false
The item is the index 0 is: Item1
Retrieving items with loop using index and size list
Index: 0 - Item: Item1
Index: 1 - Item: Item2
Index: 2 - Item: Item3
Index: 3 - Item: Item4
Retrieving items using foreach loop
Item is: Item1
Item is: Item2
Item is: Item3
Item is: Item4
Retrieving items using iterator
Item is: Item1
Item is: Item2
Item is: Item3
Item is: Item4
The arraylist after the replacement is: [Item1, NewItem, Item3, Item4]
The final contents of the arraylist are: [NewItem, Item4]
The array created after the conversion of our arraylist is: [NewItem, Item4]
最后
-
更多参考精彩博文请看这里:《陈永佳的博客》
-
喜欢博主的小伙伴可以加个关注、点个赞哦,持续更新嘿嘿!