1、什么是ArrayList

ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:

  • 动态的增加和减少元素
  • 实现了ICollection和IList接口
  • 灵活的设置数组的大小

大家知道,数组是静态的,数组被初始化之后,数组长度就不能再改变了。ArrayList是可以动态改变大小的。那么,什么时候使用Array(数组),什么时候使用ArrayList?答案是:当我们不知道到底有多少个数据元素的时候,就可使用ArrayList;如果知道数据集合有多少个元素,就用数组。

2、如何构建ArrayList

ArrayList类支持3个构造方法。

    • Arraylist()  这个构造方法构造了一个空的链表。
    • ArrayList(Collection<? extends E> c)  这个构造方法构造了一个包含指定元素集合的链表,注意,这里的字符E是一个标记,用来表示集合中元素的类型。至于具体是什么类型,需要你在使用这个构造方法的时候来指定。
    • ArrayList(int initialCapacity)  这是第三个构造方法,构造了一个指定大小但内容为空的链表。initialCapacity参数就是初始容量大小。

举例来说,如果你要创建一个空的数组链表,用来存放String类型的对象,那么你可以像下面这样做:

  ArrayList<String> list = new ArrayList<String>();

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

  ArrayList<Integer> list = new ArrayList<Integer>(7);

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

初始化ArrayList的两种方法

  方式一:
      ArrayList<String> list = new ArrayList<String>();
      String str01 = String("str01");
      String str02 = String("str02");
      list.add(str01);
      list.add(str02);
  方式二:
      ArrayList<String> list = new ArrayList<String>(){{add("str01"); add("str02");}};

3.ArrayList常用方法

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

  • 增加元素到链表中

    • boolean add(Element e)
      增加指定元素到链表尾部.
    • void add(int index, Element e)
      增加指定元素到链表指定位置.
  • 从链表中删除元素

    • void clear()
      从链表中删除所有元素.
    • E remove(int index)
      删除链表中指定位置的元素.
    • protected void removeRange(int start, int end)
      删除链表中从某一个位置开始到某一个位置结束的元素。
  • 获取链表中的元素

    • E get(int index)
      获取链表中指定位置处的元素.
    • Object[] toArray()
      获取一个数组,数组中所有元素是链表中的元素.(即将链表转换为一个数组)
  • 修改某个元素

    • E set(int index, E element)
      将链表中指定位置上的元素替换成新元素。
  • 搜索元素

    • boolean contains(Object o)
      如果链表包含指定元素,返回true.
    • int indexOf(Object o)
      返回元素在链表中第一次出现的位置,如果返回-1,表示链表中没有这个元素。
    • int lastIndexOf(Object o)
      返回元素在链表中最后一次出现的位置,如果返回-1,表示链表中没有这个元素。
  • 检查链表是否为空

    • boolean isEmpty()
      返回true表示链表中没有任何元素.
  • 获取链表大小

    • int size()
      返回链表长度(链表包含元素的个数).

4.ArrayList使用实例

 import java.util.*;

 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 为 Array
String[] 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]

5.用ArrayList类解题

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

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

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

下面就是代码:

 import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner; public class 哈夫曼树 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int m,sum=0;
int n = sc.nextInt();
ArrayList<Integer> num = new ArrayList<Integer>();
for (int i=0;i<n;i++){
num.add(sc.nextInt());
}
while(n>1){
Collections.sort(num);
m=num.get(0)+num.get(1);
sum = sum+m;
num.remove(0);
num.remove(0);
num.add(m);
n--;
}
System.out.println(sum);
}
}

ArrayList用法详解的更多相关文章

  1. ArrayList用法详解与源码分析

    说明 此文章分两部分,1.ArrayList用法.2.源码分析.先用法后分析是为了以后忘了查阅起来方便-- ArrayList 基本用法 1.创建ArrayList对象 //创建默认容量的数组列表(默 ...

  2. Java 注解用法详解——@SuppressWarnings

    转自: https://www.cnblogs.com/fsjohnhuang/p/4040785.html Java魔法堂:注解用法详解——@SuppressWarnings   一.前言 编码时我 ...

  3. @SuppressWarnings注解用法详解

    @SuppressWarnings注解用法详解 今天来谈谈@SuppressWarnings注解的作用. J2SE 提供的最后一个批注是 @SuppressWarnings.该批注的作用是给编译器一条 ...

  4. 2020了你还不会Java8新特性?(五)收集器比较器用法详解及源码剖析

    收集器用法详解与多级分组和分区 为什么在collectors类中定义一个静态内部类? static class CollectorImpl<T, A, R> implements Coll ...

  5. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  6. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  7. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

  8. mysql中event的用法详解

    一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...

  9. CSS中伪类及伪元素用法详解

    CSS中伪类及伪元素用法详解   伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...

随机推荐

  1. aop编程术语

  2. Notepad++ 多行一起编辑

      快捷方法: 鼠标:alt+滑鼠左鍵拖拉選取.鍵盤:alt+shift+方向鍵.

  3. percona xtradb cluster test

    docker run --rm -ti -e CLUSTER_NAME=test -e MYSQL_ALLOW_EMPTY_PASSWORD=1 --entrypoint="bash&quo ...

  4. Linux Debian 下LNMP服务器——nginx+mysql+php环境搭建及配置

    昨天刚给公司服务器装了LNMP服务器环境,在这里简单记录一下过程备忘. 这里我在安装的时候是用的Dotdeb源,仅供参考. 1.导入Dotdeb源,据说Dotdeb源里的软件版本比较新. 在向源中导入 ...

  5. Spark scala和java的api使用

    1.利用scala语言开发spark的worcount程序(本地运行) package com.zy.spark import org.apache.spark.rdd.RDD import org. ...

  6. 启动和关闭MySQL服务

    停止mysql服务,说来简单,但不知道的话,还真是挠头.在这和mysql入门的同学们共享:)  正确方法是,进入mysql的bin目录下,然后执行./mysqladmin -uroot -p shut ...

  7. 111. Minimum Depth of Binary Tree (Tree; DFS)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  8. Dell 1420N使用Kubuntu默认无线驱动后网络不稳定的解决方法

    前几天在我的Dell 1420N上安装了Kubuntu 13.04,装了系统软件中的私有无线网卡驱动Broadcom STA wireless driver后,虽然能上网,但是很不稳定,经常断线,非常 ...

  9. Linux buffer and cache

    A buffer is something that has yet to be "written" to disk. A cache is something that has ...

  10. code2800 送外卖

    首先,对图进行一次Floyd(g[][]是图) 1.dfs:(u是当前在的节点,d是已经走的路程) void dfs(int u,int d){ if(d>=ans)return; bool f ...