Easy Tutorial
❮ Java Object Equals String Performance ❯

Java Example - Array to Collection

Java Examples

The following example demonstrates how to convert an array to a collection using the Arrays.asList(name) method from the Java Util class:

ArrayToCollection.java File

import java.util.*;
import java.io.*;

public class ArrayToCollection {
   public static void main(String args[]) 
   throws IOException {
      int n = 5;         // 5 elements
      String[] name = new String[n];
      for(int i = 0; i < n; i++) {
         name[i] = String.valueOf(i);
      }
      List<String> list = Arrays.asList(name); 
      System.out.println();
      for(String li: list) {
         String str = li;
         System.out.print(str + " ");
      }
   }
}

The output of the above code is:

0 1 2 3 4

Java Examples

❮ Java Object Equals String Performance ❯