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: |
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.
int [] marks = {50, 45, 96, 45, 67, 87, 98, 65, 34, 99};
marks = new int [] {50, 45, 96, 45, 67, 87, 98, 65, 34, 99};
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"
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
for (int i=0; i < marks.length; i++) { System.out.print(marks[i] + " "); }
System.out.println(Arrays.toString(marks));
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] }
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.
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;
if (Arrays.equals (array1, array2)) ...
// will return TRUE if all elements are equalif (Arrays.deepEquals(array, array2)) ...
Arrays.fill(marks,100); //bingo. All marks are set to 100.
Arrays.sort(array1); //takes the array and sorts it
int index = Arrays.binarySearch(marks, 49);
The location of the first 49 is stored in "index". If the value is not found, it returns -1System.out.println(Arrays.toString(marks));
will print out the array
//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.