java中ArrayList有什么用,举例说明?

2021/6/18 9:26:56

本文主要是介绍java中ArrayList有什么用,举例说明?,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.1 ArrayList的用法 

马克-to-win:ArrayList是List接口的众多实现类其中的一个: 可以使我们快速访问元素,马克-to-win:为什么?因为它的内部组成结构就像Array一样,而且提供了可以直接访问第几个元素的方法比如下面例子中的get(index),但往其中插入和删除元素时,速度却稍慢。与LinkedList相比,它的效率要低许多。(因为LinkedList的内部像个Link, 参考数据结构)ArrayList遍历时要用到Iterator(见下)。(新手可忽略)和vector相比: (from java documentation: ArrayList is roughly equivalent to Vector, except that it is unsynchronized.()there is no synchronized keyword in the ArrayList source code.if multithread access ArrayList, you need to use synchroized keyword in your code yourself.).Vector是线程安全的,但多数情况下不使用Vector,因为线程安全需要更多的系统开销。

一个ArrayList的实例:

例:1.1.1

import java.util.ArrayList;

public class TestMark_to_win {
    public static void main(String args[]) {
        ArrayList l = new ArrayList();
        l.add("a");l.add("b");l.add("c");l.add("d");
        System.out.println(l.get(1) + "\n");
        System.out.println(l + "\n");
    }
}

 

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/102657087





这篇关于java中ArrayList有什么用,举例说明?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程