3

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....but I dont see how people adding more item in String[].

2 Answers 2

4

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] = "String One";
foo[1] = "String Two";

You can also forward declare it like that:

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

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 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<String> foo = new ArrayList<>();

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("String One");
foo.add("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 might be the right one for the job.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your clear explanation, if I let's say, give the String [] size of 999, my data won't be that much anyway, would it be faster than Array list? Some said arraylist has slower storing speed.
A simple array is generally speaking faster cause there is no resizing going on. But it really depends on your use case: What do you want to do with the data in the listview. Ist it a fixed set (String[]) or do you need to dynamically alter it ? Does the program needs to be informed abourt a change or not (use an ObservableList instead since you use a ListView). Once you know the use case you can choose the right one.
I just want to show the list when onCreate, update the list when added new item, and delete at item on finger position when holding/longclick.
Then the Observable List is your way to go. docs.oracle.com/javafx/2/collections/jfxpub-collections.htm
I'm glad i could help. Please check also the ListView docs that mention the ObservableList too: docs.oracle.com/javase/8/javafx/api/javafx/scene/control/…
2
  • 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.