Home > Tutorial > Java Array to List Example

Java Array to List Example

You can easily convert a Java Array to a Java List of that contains strings, integers, or any other data types.


Convert String Array to List

If you have a Java String Array you can easily convert that to a List using the Arrays.asList() method.

package com.jcd.java.tutorials;

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

public class JavaArrayToList 
{
	
	public static void main(String[] args) 
	{
		//Convert a string array to list
		String[] jcdStringArray = {"Java", "Code", "Depot", "Awesome", "Tutorials"};
		List<String> jcdList = Arrays.asList(jcdStringArray);
		for(String jcdString : jcdList) 
		{
			System.out.println("Each String Item: "+jcdString);
		}
	}
}
Each String Item: Java
Each String Item: Code
Each String Item: Depot
Each String Item: Awesome
Each String Item: Tutorials


Convert Integer Array to List

In order to convert a Array of Integers, you will need to iterate over the array and add each int to the List item.

package com.jcd.java.tutorials;

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

public class JavaArrayToList 
{
	public static void main(String[] args) 
	{
		//Convert a integer array to list
		int[] jcdIntegerArray = {100, 200, 300, 400, 500};
		List<Integer> jcdIntList = new ArrayList<Integer>();
		
		for(int i : jcdIntegerArray) 
		{
			jcdIntList.add(i);
		}
		
		for(int i : jcdIntegerArray) 
		{
			System.out.println("Each Integer Item: "+i);
		}
	}
}
Each Integer Item: 100
Each Integer Item: 200
Each Integer Item: 300
Each Integer Item: 400
Each Integer Item: 500



Java References:

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot