Assignment #130: Making Arrays

Code


public class MakingArrays {

    public static void main(String[] args) {

        //Here we created an integer array with 5 elements
        //int[] intArray = {4, 12, 15, 23, 100000};

        //Here we created an integer array with 5 empty spots that can be filled with any integers
        //int[] anotherIntArray = new int[5];

        //Here is one way to initialize each of those empty spots
        //anotherIntArray[0] = -11;
        //anotherIntArray[1] = 0;
        //anotherIntArray[2] = 211;
        //anotherIntArray[3] = 17;
        //anotherIntArray[4] = -1990;

        //Here we created a String array with 3 elements
        //String[] stringArray = {"Hello", ",", "world!"};

        //Here we created a String array with 3 empty spots
        //String[] anotherStringArray = new String[3];

        //You can initialize each empty spot the same as before, only with strings here
        //anotherStringArray[0] = "Mr.";
        //anotherStringArray[1] = "Joshua";
        //anotherStringArray[2] = "Davis";

        //Using arrays in output is pretty much what you would expect
        //System.out.println(intArray[0] + " + " + intArray[4] + " = " + (intArray[0] + intArray[4]));

        //Sometimes things can be a bit weird
        //Here the intArray[0] + intArray[4] are treated as strings
        //System.out.println(intArray[0] + " + " + intArray[4] + " = " + intArray[0] + intArray[4]);

        //Output with String arrays
        //System.out.println(stringArray[0] + stringArray[1] + " " + stringArray[2]);
         
        int[] intArrayPractice = {1, 2, 3};
        
        System.out.println(intArrayPractice[0] + " " + intArrayPractice[1] + " " + intArrayPractice[2]);
        
        String[] stringArrayPractice = new String[3];
        
        stringArrayPractice[0] = "A ";
        
        stringArrayPractice[1] = "string ";
        
        stringArrayPractice[2] = "array.";
        
        System.out.println(stringArrayPractice[0] + stringArrayPractice[1] + stringArrayPractice[2]);

    }
}

    

Picture of the output