Arrays Flashcards
What is an array used for?
A collection of variables of the same type organized in a way that can be easily manipulated.
Are arrays objects?
Yes
Array syntax
type arrayname[ ] = new type[size];
Base type
The element type of the array
Index
The location of an individual element in the array
Initializing an array
type arrayname[ ] = {val1, val2};
You can just initialize an array rather than using the new declaration.
What happens when you overrun the end of an array?
A run-time error
Multi dimensional array syntax
type arrayname [] [] = new type[size] [size];
Each dimension is placed in its own set of brackets.
Memory is specified for only the first (leftmost) dimension.
Array initialization for a 2D array
type arrayname[] [] = {
{Val, Val}
{Val, Val}
};
Alternative Array Declaration
type[] arrayname;
convenient when declaring multiple arrays on the same line.
arrayname1 = arrayname2;
Will the array contents be copied?
No, it will refer to the original array.
Array Length
How can it be used in iteration?
arrayname.length
It represents the number of elements an array can hold, not its actual contents.
Can be used to govern the number of iterations that take place.