ArrayList详解

1、什么是ArrayList
ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:
1.动态的增加和减少元素
2.实现了ICollection和IList接口
3.灵活的设置数组的大小
大家知道,数组是静态的,数组被初始化之后,数组长度就不能再改变了。ArrayList是可以动态改变大小的。那么,什么时候使用Array(数组),什么时候使用ArrayList?答案是:当我们不知道到底有多少个数据元素的时候,就可使用ArrayList;如果知道数据集合有多少个元素,就用数组。

2、如何构建ArrayList
ArrayList类支持3个构造方法。
1.Arraylist()  这个构造方法构造了一个空的链表。
2.ArrayList(Collection<? extends E> c)  这个构造方法构造了一个包含指定元素集合的链表,注意,这里的字符E是一个标记,用来表示集合中元素的类型。至于具体是什么类型,需要你在使用这个构造方法的时候来指定。
3.ArrayList(int initialCapacity)  这是第三个构造方法,构造了一个指定大小但内容为空的链表。initialCapacity参数就是初始容量大小。
举例来说,如果你要创建一个空的数组链表,用来存放String类型的对象,那么你可以像下面这样做:

ArrayList list = new ArrayList();**

如果你需要创建一个指定初始容量的数组链表,你可以像下面这样做:

ArrayList list = new ArrayList(7);

注意:ArrayList类只支持对象类型,不支持 基础数据类型。就是说ArrayList对象只能存放对象,不能存放基础数据类型的数据。

初始化ArrayList的两种方法
  方式一:
    ArrayList list = new ArrayList();
    String str01 = String(“str01”);
    String str02 = String(“str02”);
     list.add(str01);
    list.add(str02);
  方式二:
    ArrayList list = new ArrayList(){{add(“str01”); add(“str02”);}};

3.ArrayList常用方法
下面是总结了一些比较常用的ArrayList类成员方法:

1.增加元素到链表中
boolean add(Element e)
2.增加指定元素到链表尾部.
void add(int index, Element e)
3.增加指定元素到链表指定位置.
4.从链表中删除元素
void clear()
5.从链表中删除所有元素.
E remove(int index)
6.删除链表中指定位置的元素.
protected void removeRange(int start, int end)
7.删除链表中从某一个位置开始到某一个位置结束的元素。
8.获取链表中的元素
E get(int index)
9.获取链表中指定位置处的元素.
Object[] toArray()
10.获取一个数组,数组中所有元素是链表中的元素.(即将链表转换为一个数组)
11.修改某个元素
E set(int index, E element)
12.将链表中指定位置上的元素替换成新元素。
13.搜索元素
boolean contains(Object o)
14.如果链表包含指定元素,返回true.
int indexOf(Object o)
15.返回元素在链表中第一次出现的位置,如果返回-1,表示链表中没有这个元素。
int lastIndexOf(Object o)
16.返回元素在链表中最后一次出现的位置,如果返回-1,表示链表中没有这个元素。
17.检查链表是否为空
boolean isEmpty()
18.返回true表示链表中没有任何元素.
19.获取链表大小
int size()
20.返回链表长度(链表包含元素的个数).
4.ArrayList使用实例
复制代码
1 import java.util.*;
2
3 public class ArrayListExamples {
4
5 public static void main(String args[]) {
6 // 创建一个空的数组链表对象list,list用来存放String类型的数据
7 ArrayList list = new ArrayList();
8
9 // 增加元素到list对象中
10 list.add(“Item1”);
11 list.add(“Item2”);
12 list.add(2, “Item3”); // 此条语句将会把“Item3”字符串增加到list的第3个位置。
13 list.add(“Item4”);
14
15 // 显示数组链表中的内容
16 System.out.println("The arraylist contains the following elements: "
17 + list);
18
19 // 检查元素的位置
20 int pos = list.indexOf(“Item2”);
21 System.out.println("The index of Item2 is: " + pos);
22
23 // 检查数组链表是否为空
24 boolean check = list.isEmpty();
25 System.out.println("Checking if the arraylist is empty: " + check);
26
27 // 获取链表的大小
28 int size = list.size();
29 System.out.println("The size of the list is: " + size);
30
31 // 检查数组链表中是否包含某元素
32 boolean element = list.contains(“Item5”);
33 System.out
34 .println("Checking if the arraylist contains the object Item5: "
35 + element);
36
37 // 获取指定位置上的元素
38 String item = list.get(0);
39 System.out.println("The item is the index 0 is: " + item);
40
41 // 遍历arraylist中的元素
42
43 // 第1种方法: 循环使用元素的索引和链表的大小
44 System.out
45 .println(“Retrieving items with loop using index and size list”);
46 for (int i = 0; i < list.size(); i++) {
47 System.out.println("Index: " + i + " - Item: " + list.get(i));
48 }
49
50 // 第2种方法:使用foreach循环
51 System.out.println(“Retrieving items using foreach loop”);
52 for (String str : list) {
53 System.out.println("Item is: " + str);
54 }
55
56 // 第三种方法:使用迭代器
57 // hasNext(): 返回true表示链表链表中还有元素
58 // next(): 返回下一个元素
59 System.out.println(“Retrieving items using iterator”);
60 for (Iterator it = list.iterator(); it.hasNext()? {
61 System.out.println("Item is: " + it.next());
62 }
63
64 // 替换元素
65 list.set(1, “NewItem”);
66 System.out.println("The arraylist after the replacement is: " + list);
67
68 // 移除元素
69 // 移除第0个位置上的元素
70 list.remove(0);
71
72 // 移除第一次找到的 "Item3"元素
73 list.remove(“Item3”);
74
75 System.out.println("The final contents of the arraylist are: " + list);
76
77 // 转换 ArrayList 为 Array
78 String[] simpleArray = list.toArray(new String[list.size()]);
79 System.out.println("The array created after the conversion of our arraylist is: "
80 + Arrays.toString(simpleArray));
81 }
82 }
复制代码
输出:

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]

5.用ArrayList类解题

这道题就是我去查去总结ArrayList类的初衷了。这道题一看觉得很简单,把给出N个正数输入到数组里,排序找到最小的两个,求和,删掉最小的两个加入新求和得出的数字。重复过程即可。

我之前没有用过ArrayList类所以就在准备建数组想办法写排序,写了一会没写出来就不想写了把这道题放下了。后来问了同学,说用ArrayList做 collections.sort 排序。跟他要了代码看就会了这个题。但是感觉这个类还挺重要的吧就上网查了一些详解、使用方法,综合了许多博主的文章,写了这个 我觉得是目前自己用起来足够了的详解。

ps:吐槽一下博客园真的是比csdn看起来舒服很多啊,虽然博客皮肤很多都不同但是真的右下角没广告这一点就比再怎么整齐划一的博客好看多了啊。另外真的同样一篇文章我看了至少8+不同博主的博文(没有声明转载之类的,我看到最早的一篇是04年的之后重复的文章很多)所以,在这里我要声明一下,非原创,仅为个人综合不同博主的博文加上自己的理解研究综合而来,不妥侵删。(装作自己是个正经儿博主)

下面就是代码:

复制代码
1 import java.util.ArrayList;
2 import java.util.Collections;
3 import java.util.Scanner;
4
5 public class 哈夫曼树 {
6 static Scanner sc = new Scanner(System.in);
7 public static void main(String[] args) {
8 int m,sum=0;
9 int n = sc.nextInt();
10 ArrayList num = new ArrayList();
11 for (int i=0;i<n;i++){
12 num.add(sc.nextInt());
13 }
14 while(n>1){
15 Collections.sort(num);
16 m=num.get(0)+num.get(1);
17 sum = sum+m;
18 num.remove(0);
19 num.remove(0);
20 num.add(m);
21 n–;
22 }
23 System.out.println(sum);
24 }
25 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值