java array to arraylist

By Lokesh Gupta | Filed Under: Java ArrayList

Learn to convert ArrayList to array using toArray() method with example. toArray() method returns an array containing all of the elements in the list in proper sequence (from first to last element).

Содержание

  1. 1. ArrayList toArray() syntax
  2. 2. ArrayList toArray() example to convert ArrayList to Array
  3. 2.1. ArrayList toArray() – convert to object array
  4. 2.2. ArrayList toArray(T[] a) – convert to string array
  5. Method 1: Conversion using Arrays.asList()
  6. Method 2: Collections.addAll method
  7. Method 3: Manual way of doing things

1. ArrayList toArray() syntax

toArray() is overloaded method and comes in two forms:

  1. The first method does not accept any argument and returns the array of object type. We must iterate the objects array to find the desired element and typecast to desired class type.
  2. In second method, the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

After filling all array elements, it there is more space left in array then ‘null’ is populated in all those spare positions.

2. ArrayList toArray() example to convert ArrayList to Array

2.1. ArrayList toArray() – convert to object array

Java program to convert an arraylist to object array and iterate through array content.

2.2. ArrayList toArray(T[] a) – convert to string array

Java program to convert an arraylist to string array.

By Chaitanya Singh | Filed Under: Java Collections

In the last tutorial we have shared two methods of converting an ArrayList to Array with example. Here we are sharing three different ways to convert an Array to ArrayList. Basically we are converting an String Array to ArrayList of String type.

String array[] to ArrayList

Method 1: Conversion using Arrays.asList()

ArrayList arraylist= new ArrayList (Arrays.asList(arrayname));

In this example we are using Arrays.asList method to convert the Array to ArrayList .

Method 2: Collections.addAll method

Collections.addAll method all the array elements to the specified collection. This is how Collections.addAll method is being called. 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 to ArrayList .

String array[]=;
ArrayList arraylist = new ArrayList ();
Collections.addAll(arraylist, array);

OR

Collections.addAll(arraylist, new Item(1), new Item(2), new Item(3), new Item(4));

Example

Method 3: Manual way of doing things

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

Following methods can be used for converting ArrayList to Array:

Method 1: Using Object[] toArray() method


Syntax:

  • It is specified by toArray in interface Collection and interface List
  • It overrides toArray in class AbstractCollection
  • It returns an array containing all of the elements in this list in the correct order.

Note: toArray() method returns an array of type Object(Object[]). We need to typecast it to Integer before using as Integer objects. If we do not typecast, we get compilation error. Consider the following example:

It is therefore recommended to create an array into which elements of List need to be stored and pass it as an argument in toArray() method to store elements if it is big enough. Otherwise a new array of the same type is allocated for this purpose.

Method 2: Using T[] toArray(T[] a)

Note that the there is an array parameter and array return value. The main purpose of passed array is to tell the type of array. The returned array is of same type as passed array.

  • If the passed array has enough space, then elements are stored in this array itself.
  • If the passed array doesn’t have enough space, a new array is created with same type and size of given list.
  • If the passed array has more space, the array is first filled with list elements, then null values are filled.

It throws ArrayStoreException if the runtime type of a is not a supertype of the runtime type of every element in this list.

Источник: computermaker.info

Техника и Гаджеты
Добавить комментарий