How to Interconvert List and Array in Java


In this example we will show how to interconvert list and array in Java.

Source Code

1) List To Array

package com.beginner.examples;

import java.util.*;

public class List2ArrayExample {

	public static void main(String[] args) {

		List list = new ArrayList();    
		list.add("AA");    
		list.add("BB");    
		list.add("CC");
		list.add("DD");
		
		Object[] obj = list.toArray();
		System.out.println(Arrays.toString(obj));    
    }
}

Output:

[AA, BB, CC, DD]

2) Array To List

package com.beginner.examples;

import java.util.*;

public class Array2ListExample {

	public static void main(String[] args) {

		String[] string=new String[]{"AA","BB","CC","DD"};
		List list= Arrays.asList(string);
		System.out.println(list);
    }
}

Output:

[AA, BB, CC, DD]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments