How to Convert Primitive Array to List Object in Java

Convert Primitive Array to List Object in Java

In this tutorial, I am sharing simple program about, How to Convert Primitive Array to List Object in Java. The program has been tested with environment and output is shared in the same post.

ArrayProgramExample.java

package com.dineshkrish.array;

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

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class ArrayProgramExample {

	public static List<Integer> convert(int[] numbers) {

		// Defining List Object
		List<Integer> numberList = new ArrayList<Integer>();

		if (numbers.length > 0) {

			// Iterating int array
			for (int i = 0; i < numbers.length; i++) {

				// Adding elements to list
				numberList.add(numbers[i]);
			}
		}

		return numberList;
	}

	public static void main(String[] args) {

		int[] values = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

		// Method Invocation
		List<Integer> numberList = convert(values);

		// Printing the list object
		System.out.println(numberList);
	}
}

Output

—————

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

References

1. Java API Documentaion

Hello everyone, I am a Founder of dineshkrish.com.