Learn Java by Example: Java Program to Convert the ArrayList to an array and vice versa

Java Program to Convert the ArrayList to an array and vice versa

In this example, we will learn to convert the arraylist into an array and vice versa in Java.

 


Example 1: Convert the Arraylist into Array


import java.util.ArrayList;

class Main{
  public static void main(String[] args){
    ArrayList<String> languages= new ArrayList<>();

    // Add elements in the arraylist
    languages.add("Java");
    languages.add("Python");
    languages.add("JavaScript");
    System.out.println("ArrayList: " + languages);

    // Create a new array of String type
    String[] arr = new String[languages.size()];

    // Convert ArrayList into the string array
    languages.toArray(arr);
    System.out.print("Array: ");
    for(String item:arr) {
      System.out.print(item+", ");
    }
  }
}

Output

ArrayList: [Java, Python, JavaScript]
Array: Java, Python, JavaScript,

In the above example, we have created an arraylist named languages. Notice the line,

languages.toArray(arr);

Here, the toArray() method converts the arraylist languages into an array. And stores it in the string array arr.

Note: If we don’t pass any argument to the toArray() method, the method returns an array of the Object type.


Example 2: Convert Array to ArrayList


import java.util.Arrays;
import java.util.ArrayList;

class Main{
  public static void main(String[] args){

    // create an array
    String[] array = {"Java", "Python", "C"};
    System.out.println("Array: " + Arrays.toString(array));

    // convert array to arraylist
    ArrayList<String> languages= new ArrayList<>(Arrays.asList(array));

    System.out.println("ArrayList: " + languages);

  }
}

Output

Array: [Java, Python, C]
ArrayList: [Java, Python, C]

In the above example, we have created an array of String type. Notice the expression,

Arrays.asList(array)

Here, the asList() method of the Arrays class converts the specified array into the arraylist.

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.