Arrays
Some static things about arrays:
- Always always always start at index 0.
- Array has just one name.
- Array is an ordered list of values with unique numeric index.
- These values are known as array elements.
- The type of the values stored within the array has to be same.
- Size of array, n is indexed from 0 --> n-1.
- The size you define for array is fixed.
- Array is considered to be an object thus, array of integers, Chars and String objects can be created.
- When an initializer list is used you 'don't need'
a) to specify the size of the array
b) or use new operator
One-dimensional Array
- Declare an array
int[] labscores = new int[25]
- Value is referenced via array name followed by index in brackets.
labscores[2]
- Way to initialize and instantiate array:
int[] labScores = {20,19,...,16,18.5,20};
Notice the usage of brackets, comma and semi-colon.
Let us see what wonders two-dimensional arrays can do.
Two-dimensional arrays:
- Table of elements represented by rows and columns. No more complicated than that.
- Said to be array of arrays. (Don't keep repeating this otherwise you will never come out of recursive thinking).
- Declare a two dimensional array
int[][] labScores = new int[4][25]
- Value is referenced using two index values
labScores[2][24]
- Way to initialize and instantiate array:
int[][] labScores = { {14, 20, 28, 19, 20}, {17, 19.5, 20, 20, 20}};
Notice the use of brackets, commas and semi-colon.
Make sure the list is of the same size. If you put five elements in one then second should have the same. It is not necessary to have same # of lists as the elements. This example has two lists but the elements are five.
Mathematicians, think of this as matrix.