Java ArrayList to Integer Array Example

The following example code shows how to convert a Java ArrayList of Integers to a Java Integers Array using a standard for loop to move the int elements. Unfortunately there is no one method to accomplish this task.


Java ArrayList to Integer Array

package com.jcd.java.tutorials;

import java.util.ArrayList;

public class JavaListToArrayInteger 
{

	public static void main(String[] args) 
	{

		//Convert ArrayList to Integer Array
		ArrayList<Integer> jcdIntegerList = new ArrayList<Integer>();
		jcdIntegerList.add(100);
		jcdIntegerList.add(200);
		jcdIntegerList.add(300);
		jcdIntegerList.add(400);
		
		int[] jcdIntArray = new int[jcdIntegerList.size()];
		
		int myInt = 0;
		
		for (Integer jcdInt : jcdIntegerList) {
			jcdIntArray[myInt++] = jcdInt;
	    }
		
		for(Integer myInteger : jcdIntArray) 
		{
			System.out.println(myInteger);
		}


	}

}

Java ArrayList to Integer Array Sample Output

100
200
300
400



Java References:

Add a Comment

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