Lecture 8: Arrays

You must read  the textbook p165-174 or Java Tutorial arrays(and its links)
This section covers these topics: Here's a simple program, named ArrayDemo, the program creates an array, puts some values in it, and displays the values.
public class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declare an array of integers

anArray = new int[10]; // create an array of integers

// assign a value to each array element and print
for (int i = 0; i < anArray.length; i++) {
anArray[i] = i;
System.out.print(anArray[i] + " ");
}
System.out.println();
}
}



Declaring a Variable to Refer to an Array

Example:

What does it do?


Creating an Array

Example:

anArray = new int[10];  // create an array of integers

Syntax:

new elementType[arraySize]

Error message:

If the new statement was ommitted from the sample program, the following error message would display
         ArrayDemo.java:4: Variable anArray may not have been initialized.

What it does?


Accessing an Array Element

Example:

Note:


Getting the Size of an Array

To get the size of an array, we can use
arrayname.length

Example

anArray.length;
anArrayOfString.length;

Array Initializers


Arrays of Objects

public class ArrayOfStringsDemo {
public static void main(String[] args) {
String[] anArray = { "String One", "String Two", "String Three" };

for (int i = 0; i < anArray.length; i++) {
System.out.println(anArray[i].toLowerCase());
}
}
}


public class ArrayOfStringsDemo2 {
public static void main(String[] args) {
String[] anArray = new String[3];
String[0] = "String One";
String[1] = "String Two";
String[2] = "String Three";
for (int i = 0; i < anArray.length; i++) {
System.out.println(anArray[i].toLowerCase());
}
}
}



What's wrong with this program?

 public class BankAccountTest {
public static void main(String[] args) {
BankAccount[] accountArray = new BankAccount[3];
for(int i = 0; i < accountArray.lenght; i++) {
System.out.println(accountArray[i].getName());
}
}
}

Answer

public class BankAccountTest {
public static void main(String[] args) {
BankAccount[] accountArray = new BankAccount[3];
accountArray[0] = new BankAccount("Amy", 500000);
accountArray[1] = new BankAccount("Benny", 3456);
accountArray[0] = new BankAccount("Charles", 100000);


for(int i = 0; i < accountArray.lenght; i++) {
System.out.println(accountArray[i].getName());
}
}
}
You have to initiate the elements in the array. Otherwise you will get a null pointer excepetion.


Arrays  of  Arrays

This example creates an array and uses an initializer to populate it with four sub-arrays.
public class ArrayOfArraysDemo {
public static void main(String[] args) {
String[][] cartoons =
{
{ "Flintstones", "Fred", "Wilma", "Pebbles", "Dino" },
{ "Rubbles", "Barney", "Betty", "Bam Bam" },
{ "Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie", "Astro" },
{ "Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne" }
};


for (int i = 0; i < cartoons.length; i++) {
System.out.print(cartoons[i][0] + ": ");
for (int j = 1; j < cartoons[i].length; j++) {
System.out.print(cartoons[i][j] + " ");
}
System.out.println();
}
}
}




Output

Flintstones: Fred Wilma Pebbles Dino
Rubbles: Barney Betty Bam Bam
Jetsons: George Jane Elroy Judy Rosie Astro
Scooby Doo Gang: Scooby Doo Shaggy Velma Fred Daphne


As with arrays of objects, you must explicitly create the sub-arrays within an array. So if you don't use an initializer, you need to write code like the following, which you can find in: ArrayOfArraysDemo2
public class ArrayOfArraysDemo2 {
public static void main(String[] args) {
int[][] aMatrix = new int[4][];

//populate matrix
for (int i = 0; i < aMatrix.length; i++) {
aMatrix[i] = new int[5]; //create sub-array
for (int j = 0; j < aMatrix[i].length; j++) {
aMatrix[i][j] = i + j;
}
}


//print matrix
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
System.out.print(aMatrix[i][j] + " ");
}
System.out.println();
}
}

Output

0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

Summary of Arrays

After this class you should know