Java Tutorial/Arrays

Arrays are collections of other things. Their structure is as follows:

[0][1][2][3]...

Each [x] section is one of the "thing". As you can see, they begin not at 1 but at 0. An example of declaring an integer array:

int size = 4;
int[] myIntArray = new int[size];

More ways to DeclareEdit

An array can be declared like this:

type[]name;

It can be initialized like this:

name = new type[size];

You can combine those steps, but the types always have to match up:

int[] name = new int[4];
//OR...
int name[] = new int[4];

Setting and Accessing ValuesEdit

You can set a value in an array like this:

String[] myStringArray = new String[3];
myStringArray[0] = "Hello"; //set...
System.out.println(myStringArray[0]); //access

Each element in the array is just like a variable! You can do anything to it that you can do to a variable. Remember how we learned about for loops in the last lesson? Well, that can be used to set things in arrays:

double[] myDoubleArray = new double[50]; //we don't want to set each one on a new line...
for(int i = 0; i < myDoubleArray.length; i++) { //arr.length means the length of array named "arr"
    myDoubleArray[i] = i * 1.5; //set the element at i; because i increases up to the length, all
                                //values will be set; also, the value is set like 0, 1.5, 3, 4.5, ...
}

Arrays of ArraysEdit

You can make 2D arrays, 3D arrays, and even 4D and 5D arrays!! Here is how:

int[][] my2DArray = new int[6][5]; //envision it as having 6 rows of 5 columns, or elements, each; it's an array within an array!

Then, you can use a simple double for loop to set them:

double[][] my2DDoubleArray = new double[50][50]; //we don't want to set each one on a new line...
for(int i = 0; i < my2DDoubleArray.length; i++) { //arr.length means the length of array named "arr"
    for(int j = 0; j < my2DDoubleArray[i].length; j++) {
        my2DDoubleArray[i][j] = i * j;
    }
}

Using ArgumentsEdit

Have you been dying to know what the String args[] within the main method is? Well, if you have (and also if you haven't, because you should know), read onwards!

It is just an array with the ones you have been playing with (hopefully) in the rest of this lesson. Try executing the command:

java SomeClass hello hello hello

Notice how it doesn't say anything. Where is the "hello hello hello" going? Well, it is going into the arguments (args) array.

The array would be something like:

["hello"]["hello"]["hello"] << length of 3

You can access these in your class. Here is an example that prints the arguments that you send into it:

public class PrintArgs {
    public static void main(String args[]) {
        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
}

With the command java PrintArgs hello, the program would output a single line saying "hello".

Uses of ArraysEdit

So... what can you do with these things? Well, you can make (as an example) the basics of a bank account now!

public class BankAccounts {
    public static void main(String args[]) {
        int[] bankAccounts = new int[args.length]; //each element in bankAccounts will be the amount of money in the account
        //set the account to what the user specified:
        for (int i = 0; i < args.length; i++) {
            bankAccounts[i] = Integer.parseInt(args[i]); //Integer.parseInt transforms it's parameter into an integer
        }
        
        //now you can do stuff with the accounts!
    }
}

ExercisesEdit

  • Make the bank account program print the accounts it received
  • Now edit it so that it removes 1 from each account, and then prints the values

An example of a valid answer:

public class BankAccounts {
    public static void main(String args[]) {
        int[] bankAccounts = new int[args.length]; //each element in bankAccounts will be the amount of money in the account
        //set the account to what the user specified:
        for (int i = 0; i < args.length; i++) {
            bankAccounts[i] = Integer.parseInt(args[i]); //Integer.parseInt transforms it's parameter into an integer
            bankAccounts[i]--; //-- decreases the variables value by 1
        }

        for (int i = 0; i < bankAccounts.length; i++) {
            System.out.println(bankAccounts[i]);
        }
    }
}
Previous: Methods Up: Java Tutorial Next: String and Character Manipulation