Arrays

Array Declaration

Array declaration int [] marks; or int marks[];
Assign a block of memory to the array
(instantiate)
marks = new int[10];
Done in a single step int [] marks = new int[10];
X. You cannot add in the size here: never: int marks[10];

Filling an Array

The array may be filled with default values during declaration. The list of values must be enclosed between curly brackets and be separated by commas.

The number of items is determined by the number of values, not by a size that you type in.

Or, just fill with numbers like this

 	marks[0]= 2;
or
 	for (int i=0; i < marks.length; i++)
 		marks[i]=0;

Size of Array an array object has a special public variable called length, which stores the size of the array.

System.out.println(marks.length); 	//will print out 10

If you try to access an element that doesn't exist you get the error "ArrayIndexOutOfBoundsException"

Enhanced for loop

Also known as "for-each loop"

These are specially for arrays and collections of objects (arraylists).
These types of loops are really handy for going through arrays, but they seem to have problems with 2D arrays.

for (int mm : marks) {			// "int" must be the same type as the array type
	System.out.print(mm + " ");	// you can just use the variable name mm, instead of marks[i]
}

Note: The enhanced for loop can only be used for retrieving data from an array, not for putting data into an array.
This is refering to an array of primitive data types. If you have an array of objects, you can use an enhanced for loop to acces each object and then change the properties of that object.

There will be another example of enhanced for loop when we get to arraylists of objects

Printing out an Array

Method 1: using a for loop

for (int i=0; i < marks.length; i++) {	
	System.out.print(marks[i] + " ");
}

Method 2: using the Arrays class

System.out.println(Arrays.toString(marks));	

Method 3: using enhanced for loop

for (int mm : marks) {			// "int" must be the same type as the array type
	System.out.print(mm + " ");	// you can just use the variable name mm, instead of marks[i]
}

2-D Arrays

Declaration and instantiation separately int[][] arr; arr = new int [3][4];
Declaration and instantiation together int[][] A = new int [3][4];
These statements would create an array with 3 rows and 4 items in each row.

Filling an Array During Initialization: ** This determines what the size is.

int[][] A = { {6, 7, 4, 3}, {0, 4, 7, 2}, {4, 2, 1, 6} };

A 2-D array is actually one array containing other arrays, so you can make jagged arrays, but why would you want to?

System.arraycopy(...) will copy all or part of one array to another.

An array is not a primitive data type. So, you cannot test to see if array1 == array2. array1 == array2 only if the two variable names point to the same location in memory. To test if two arrays (or any other objects) are equal, you have to use the Arrays class.

Class "Arrays" — tools to work with arrays

Note: "Arrays" is not an array or an object, it is a class of methods that will do things to arrays which you have created

import java.util.Arrays;

Arrays of objects

//instance variables JButton btns = new JButton[6];
This just declares them. You MUST put something here to define the size.
//in a function or constructor for (int i=0; i < btns.length; i++) { btns[i] = new JButton("Button #" + i); //this initializes them ... }
Why does "new JButton" appear twice? The first time it's required to make the objects, essentially the same as creating JButton b1, b2, b3, b4, b5, b6.
Because these are objects that have not actually been initialized, they are all set to null. The constructor for these objects has to be run ONE AT A TIME. You cannot run a constructor on a whole array of objects at once. That's why the second "new JButton" is needed.