Home  >  Article  >  Java  >  How many ways are there to convert an array to ArrayList in Java?

How many ways are there to convert an array to ArrayList in Java?

WBOY
WBOYforward
2023-09-01 10:01:071156browse

How many ways are there to convert an array to ArrayList in Java?

By adding each element of the array

The add() method of the ArrayList class accepts one element and adds it to the current array list. The steps to convert an array to a list of arrays using this method are as follows:

  • Get the string array.

  • Create an empty ArrayList object.

  • Add each element of the array to the ArrayList.

  • Print the contents of the array list.

Example

Demonstration

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayToArrayList {
   public static void main(String args[]) {
      String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"};
      ArrayList<String> arrayList = new ArrayList<String>();
      for(int i = 0; i < stringArray.length; i++) {
         arrayList.add(stringArray[i]);
      }
      System.out.println("Contents of the array list: ");
      Iterator it = arrayList.iterator();
      while(it.hasNext()) {
         System.out.print(it.next());
      }
   }
}

Output

Contents of the array list:
JavaFX
Java
WebGL
OpenCV
OpenNLP
JOGL
Hadoop
HBase
Flume
Mahout
Impala

Using the asList() method

ArrayList The asList() method of the class accepts an array and returns a List object. To convert an array to an ArrayList, you need to perform the following steps:

  • Get the desired array.

  • By passing an array as a parameter to the asList() method and retrieving the List object.

  • Instantiate an ArrayList by passing the list object obtained in the previous step to the ArrayList class.

  • Print the contents of ArrayList.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class ArrayToArrayList {
   public static void main(String args[]) {
      String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"};
      List <String> list = Arrays.asList(stringArray);
      ArrayList<String> arrayList = new ArrayList(list);
      System.out.println("Contents of the array list: ");
      Iterator it = arrayList.iterator();
      while(it.hasNext()) {
         System.out.println(it.next());
      }
   }
}

Output

Contents of the array list:
JavaFX
Java
WebGL
OpenCV
OpenNLP
JOGL
Hadoop
HBase
Flume
Mahout
Impala

Use the addAll method of the Collection class

The addAll() method of the collection class accepts an array Takes a list object and an array as parameters and adds the elements of the given array to the array list. Therefore, to use this object to convert an array to an ArrayList, the following steps need to be performed:

  • Get the array.

  • Create an empty ArrayList object.

  • The method is called by passing the array list and the array as parameters to the addAll() method of the Collections class.

  • Print the contents of the array list.

Example

Demonstration

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
public class ArrayToArrayList {
   public static void main(String args[]) {
      String stringArray[] = {"JavaFX", "Java", "WebGL", "OpenCV", "OpenNLP", "JOGL", "Hadoop", "HBase", "Flume", "Mahout", "Impala"};
      ArrayList<String> arrayList = new ArrayList();
      Collections.addAll(arrayList, stringArray);
      System.out.println("Contents of the array list: ");
      Iterator it = arrayList.iterator();
      while(it.hasNext()) {
         System.out.println(it.next());
      }
   }
}

Output

Contents of the array list:
JavaFX
Java
WebGL
OpenCV
OpenNLP
JOGL
Hadoop
HBase
Flume
Mahout
Impala

The above is the detailed content of How many ways are there to convert an array to ArrayList in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete