Java array FAQ: How do you create an array of Java int
values (i.e., a Java “int array”)?
Answer: There are several ways to define an int array in Java; let’s take a look at a few examples.
1) Declare a Java int array with initial size; populate it later
If you know the desired size of your array, an you'll be adding elements to your array some time later in your code, you can define a Java int array using this syntax:
// (1) create a java int array int[] intArray = new int[3]; // (2) some time later ... assign elements to the array intArray[0] = 1; intArray[1] = 2; intArray[2] = 3; // (3) print our java int array for (int i=0; i<intArray.length; i++) { System.out.println(intArray[i]); }
2) Declare an int array and populate its elements
Depending on your needs, you can also create your int array using this syntax:
// (1) define your java int array int[] intArray = new int[] {4,5,6,7,8}; // (2) print the java int array for (int i=0; i<intArray.length; i++) { System.out.println(intArray[i]); }
3) A complete Java int array example
Sometimes it helps to see source code used in a complete Java program, so the Java class/program below demonstrates the different Java int array examples.
The method named intArrayExample
shows our first example. Then, to demonstrate the similiarity between an int array and a String array, the method named stringArrayExample
shows how a Java String
array is typically created and populated. Finally, the method named intArrayExample2
shows our secondint
array example (as shown above).
Here's the source code for my sample Java class:
/** * Demonstrates several Java array examples, including a * Java int array, and a Java String array. * Created by Alvin Alexander, <a href="http://alvinalexander.com" title="http://alvinalexander.com">http://alvinalexander.com</a>. */ public class JavaIntArrayExample { public static void main(String[] args) { new JavaArrayExample(); } public JavaArrayExample() { intArrayExample(); stringArrayExample(); intArrayExample2(); } /** * Create an int array, then populate the array, * and finally print each element in the int array. */ private void intArrayExample() { int[] intArray = new int[3]; intArray[0] = 1; intArray[1] = 2; intArray[2] = 3; System.out.println("intArray output"); for (int i=0; i<intArray.length; i++) { System.out.println(intArray[i]); } } /** * Create a String array, then populate the array, * and finally print each element in the int array. */ private void stringArrayExample() { String[] stringArray = new String[3]; stringArray[0] = "a"; stringArray[1] = "b"; stringArray[2] = "c"; System.out.println("stringArray output"); for (int i=0; i<stringArray.length; i++) { System.out.println(stringArray[i]); } } /** * Create a Java int array and populate it in one step. * Then get the array length and print each element in the array. */ private void intArrayExample2() { int[] intArray = new int[] {4,5,6,7,8}; System.out.println("intArray output (version 2)"); for (int i=0; i<intArray.length; i++) { System.out.println(intArray[i]); } } }