Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-09-01 10:01:07
forward
1226 people have browsed it

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());
      }
   }
}
Copy after login

Output

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

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());
      }
   }
}
Copy after login

Output

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

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());
      }
   }
}
Copy after login

Output

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template