Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between String[] and listArray<String>

Like the title, I would like to know the differences between String[] and ListArray[String], are they same to some extent.

like image 782
HeikiCyan Avatar asked Dec 01 '12 22:12

HeikiCyan


2 Answers

An array String[] cannot expand its size. You can initialize it once giving it a permanent size:

String[] myStringArray = new String[20]();
myStringArray[0] = "Test";

An ArrayList<String> is variable in size. You can add and remove items dynamically:

ArrayList<String> myStringArrayList = new ArrayList<String>();
myStringArrayList.add("Test");
myStringArrayList.remove(0);

Furthermore, you can sort, clear, addall, and a lot more functions you can use while using an ArrayList.

like image 121
nhaarman Avatar answered Oct 28 '22 13:10

nhaarman


String[] is an array of Strings while ArrayList is a generic class which takes different types of objects (here it takes Strings). Therefore you can only perform normal array operations with String[]. However, you can use additional, convenient utilities such as isEmpty(), iterator, etc with ArrayList since it also implements Collection Interface.

like image 25
clouddreams Avatar answered Oct 28 '22 13:10

clouddreams