ArrayList vs String[], How and when to use?

huangapple go评论73阅读模式
英文:

ArrayList<String> vs String[], How and when to use?

问题

我正在学习在Android应用程序上创建ListView,我想了解其中的这两种方式,一些示例使用String[],而其他一些使用ArrayList... 但是我看不出人们如何向String[]中添加更多项目。

英文:

I am learning about making a ListView on Android app, and I want to know about these two, some examples use String[], and some use ArrayList<String>....but I dont see how people adding more item in String[].

答案1

得分: 3

String[] 是一个原始数据类型,是一个普通的简单数组,具有固定大小,必须在编译时知道大小,不能在运行时更改。

考虑:

String[] foo = new String[2];

在这里,您声明了一个简单的字符串数组,可以容纳2个字符串。
让我们向其中添加内容:

foo[0] = "String One";
foo[1] = "String Two";

您也可以像这样提前声明它:

String[] foo = {"String One", "String Two"};

您可以更改foo的内容,但无法添加另一个字符串,也无法删除它,因为它具有固定大小。

另一方面,ArrayList<T> 是一个动态列表,您可以随时在程序中添加或删除类型为T的项目。
大小不需要在编译时定义,并且ArrayList的内容可以在运行时添加或删除。它是这两者中最灵活的数据结构。

考虑:

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

这声明了动态数组。它具有可变大小,无需在初始化时定义元素的数量。您可以随时在运行时添加或删除元素。

请注意:

foo.add("String One");
foo.add("String Two");

可以随时在程序中添加。删除元素也是一样。由于ArrayList是一个(泛型)类,它还具有一些普通数组没有的有用方法!请还要阅读关于Java泛型的内容,以获得完整的图片。

因此,这取决于您希望对ListView执行什么操作。如果您只希望显示一组固定的字符串,则使用String[],或者如果您需要通过添加或删除数据在视图中动态更改内容,则ArrayList<T>可能更适合此任务。

英文:

String[] is a primitive data type, a plain simple array with a fixed size which has to be known at COMPILE TIME and can't be changed at RUNTIME.

Consider:

String[] foo = new String[2];

Here you declare a simple string array which can hold 2 strings.
Let's add stuff to it:

foo[0] = &quot;String One&quot;;
foo[1] = &quot;String Two&quot;;

You can also forward declare it like that:

String[] foo = {&quot;String One&quot;, &quot;String Two&quot;};

You can change the contents of foo but you can't add another string nor remove it since it has a fixed size.

At the other hand, ArrayList<T> is a dynamic list where you can add or remove items of type T at any time in your program.
The size does not need to be defined at COMPILE TIME and the contents of a Arraylist can be added or removed at RUNTIME . It is the most flexible data structure of the two.

Consider:

ArrayList&lt;String&gt; foo = new ArrayList&lt;&gt;();

That declares the dynamic array. It has a variable size and there is no need to define the number of elements upon initalisation. You can add or remove elements to/from it as you like, any time at RUNTIME.

Note that:

foo.add(&quot;String One&quot;);
foo.add(&quot;String Two);

can be added at any time in your program. Same goes for removing elements. Since ArrayList is a (generic) class, it has also a bunch of helpfull methods that a plain array does not have !
Please also read about Java generics to get the full picture.

So it depends what you want to do with the ListView. If you just want to have a fixed set of strings displayed then go for String[], or you need to change the content of the view dynamically by adding or removing data, then an ArrayList<T> might be the right one for the job.

答案2

得分: 2

  • 首先,String[] 属于传统数组,意味着数组所包含的数据大小是固定的,无法更改。而 ArrayList 则不同,它的大小可以随着元素数量的增长而增加。此外,ArrayList 有一组方法用于访问和修改元素。但是,ArrayList 只能存储对象,存储速度较慢。

  • 关于为什么在 ListView 或 RecyclerView 中,大多数情况下我们使用 ArrayList 而不是数组,是因为 ArrayList 具有扩展的能力。例如,在 NoteApp 中,如果使用数组,必须为笔记设置固定的分配数量,但如果添加新的笔记,必须以编程方式进行更改。它无法自适应大小以容纳新的笔记。因此,ArrayList 适用于这种情况。

英文:
  • First of all, The String[] is belong to the conventional array which means that this data size that the array is contained is fixed and cannot be changed. Otherwise, the ArrayList is not. It can grow the size as the number elements grows. Moreover, ArrayList has a set of methods to access elements and modify them. But, the ArrayList just can store Object and the storing speed is more slower.

  • For the answer why in a ListView or RecyclerView, most of cases, we use the ArrayList instead of Array because of the ability of extension in ArrayList. For example, in the NoteApp, if you use the Array, you must set the fixed allocate numbers for the notes, but if you adding the new note, you must change it programmatically. It cannot adapt the size to contain the new note. So that, the ArrayList is suitable for this case.

huangapple
  • 本文由 发表于 2020年10月18日 10:46:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64409230.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定