arrays

primitive arrays and object arrays

Create a basic array in Java

public class CreateAnArray{    
    public static void main(String[] args){
        // Creates a new array of Strings with a length of 3
        // This length cannot be changed later
        String[] myStringArray = new String[3];
        myStringArray[0] = "Hello"; // Java array indicies start at 0
        myStringArray[1] = "World";
        myStringArray[2] = "!"; // The array is now full
        try{
            myStringArray[3] = "This will cause an error."; // Index 3 requires an array 
            // of size 4 or greater
        }
        catch(ArrayIndexOutOfBoundsException out){
            System.out.println("Java arrays cannot be expanded.");
        }
        // Print out "Hello World!" to the console
        System.out.println(myStringArray[0]+" "+myStringArray[1]+" "+myStringArray[2]);
    }
}

Java create an array with initial values

public class CreateArrayWithValues {
    public static void main(String[] args){
        // Initializes an array of Strings with values
        String[] myArray = {"this", "array", "has", "six", "initial", "values"};
        System.out.println("myArray.length = "+myArray.length);
        // Print out each value of myArray
        for(int index = 0; index < myArray.length; index++){
            System.out.print(myArray[index]);
        }
    }
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow