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();
}
}
anArray before the name refers to an array.anArray = new int[10]; // create an array of integers
new elementType[arraySize]
new statement was ommitted from the sample program,
the following error message would displaynull.arrayname.length
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());
}
}
}
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());
}
}
}
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.
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();
}
}
}
| 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 |
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();
}
}
| 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 |
new operator (e.g. array = new
int[10];)length
attribute. (e.g. array.length)