Arraylist in java

Author of this article has published varieties of articles in the stream of java software development. In this post, you will learn the way to create an ArrayList from an Array. Author has begun the post by defining Array and ArrayList. You will also learn the steps to convert an Array to ArrayList in this guide.

Features of an Array

  • The Array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data.
  • Array is static in size that is fixed length data structure, one cannot change the length after creating the Array object.
  • resize() operation :Auto resize of ArrayList lowers the performance as temporary array is used by the ArrayList to copy elements from old to new array.
  • add() or get() operation :Adding an element or receiving it from the ArrayList object or array is similar in performance because for ArrayList object, such operations run in regular time
  • We can use for loop or for each loop to iterate through array.
  • Array object has the length variable which returns the length of the array.
  • In array we insert elements using the assignment operator.
  • Array can be Single Dimensional and multi-dimensional array
  • Array stores Primitive data types as well as Objects.
  • Java is that you cannot use Generics along with Array.
  • Array in java is index based; first element of the array is stored at 0 index.
  • An array is an Object. So, just as you can synchronize on any Object, you can sync an array.>

Features of an ArrayList

  • The ArrayList class extends AbstractList and implements the List interface.
  • Java ArrayList class uses a dynamic array for storing the elements.
  • Every ArrayList object contains instance variable capacity that shows the ArrayList size. ArrayList expands automatically once elements are added to it.
  • resize() operation: Arraylist is supported by Array when you resize it as it uses the System.arrayCopy(src, destPos, srcPos, length, dest) method.
  • Using iterator for iterating via ArrayList is another thing we can do. ArrayList class’s iterator returns these iterators and method of listiterator fails fast.
  • The size() method offers the length of the ArrayList.
  • We can insert elements into the arraylist object using the add () method.
  • You will always find ArrayList in single dimensional. Both ArrayList and Array can have duplicate elements.
  • Both can store null values and uses index to refer to their elements.<
  • add () or get() operation :adding or receiving an element from the ArrayList or Array object has matched performance. These operations run in regular time.
  • ArrayList allows you to use Generics to ensure type-safety. ArrayList Stores Objects and arrayList allow random access.
  • ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
  • Java ArrayList class is non-synchronized.

 

-> Converting an Array to ArrayList

Here we are sharing three different ways to convert Array to ArrayList.
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

Syntax:

ArrayList<T> arraylist= new ArrayList<T> (Arrays.asList (arrayname));
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;

public class Example1 {

public static void main(String[] args) {

/* Array Declaration and initialization */

String countryNames[] = { “India”, “Russia”, “US”, “France” };

/* Array to ArrayList conversion */

ArrayList<String> countrylist = new ArrayList<String> (Arrays.asList (countryNames));

/* adding new elements to the converted List */

countrylist.add(“China”);

/* Final ArrayList content display using for */

for (String str : countrylist) {

System.out.println(str);

}

}

}

Output:

India
Russia
US
France
China

Collections.addAll() method all the array elements to the specified collection.
It does the same as Arrays.asList method however it is much faster than it so performance wise this is a best way to get the array converted toArrayList

Syntax:

Collections.addAll(arraylist, new Item(1), new Item(2), new Item(3));
public class Example2 {

public static void main(String[] args) {

/* Array Declaration and initialization */

String cityName[] = { “Delhi”, “Paris”, “Chicago”, “Melbourne” };

/* Array to ArrayList conversion */

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

Collections.addAll(citylist, cityName);

/* Adding new elements to the converted List */

citylist.add(“Beijing”);

/* Final ArrayList content display using for */

for (String str: citylist) {

System.out.println(str);

}

}

}

Output :

Delhi

Paris

Chicago

Melbourne

Beijing

Method 3: Manual way of doing things

We can also add all the array’s element to the arraylist manually. Below example of manual conversion.

public class Example3 {

public static void main(String[] args) {

/* ArrayList declaration */

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

/* Initialized Array */

String array[] = { “Text1”, “Text2”, “Text3”, “Text4” };

/* array.length returns the current number of elements present in array*/

for (int i = 0; i < array.length; i++) {

arraylist.add(array[i]);

}

/* ArrayList content */

for (String str : arraylist) {

System.out.println(str);

}

}

}

Output :

Text1

Text2

Text3

Text4

Java software development expert has shared this post with entire java community, programmers, and developers. The purpose of this post is to make them learn about Array and ArrayList and the ways to convert an Array to ArrayList. 

Author Bio

James Warner is a highly qualified digital marketing and entrepreneurship. I’m a contributing editor of NexSoftsys for many years in a various role including editor Technology, Health & Media editor and also working as a freelance. You can contact me on Facebook or Follow me on Twitter or add your circle Google+

Have something to add in tutorial?? Please share in comments.

Follow us for more hacking tutorials on Facebook, Google Plus and Twitter.

NO COMMENTS