8 - Arrays Flashcards
An array
is a simple but powerful programming language construct use to group and organize data.
An array of size N is indexed…
from 0 to N-1.
In Java, an array is an object …
that must be instantiated.
Int[ ] (type of array) height (name of array) =
new int[11] (size of array)
Automatic bounds checking
ensures that the index is in range for the array being referenced.
Int[ ] grades is better than
Int grades [ ] but both are acceptable
An initializer list can be used to instantiate an array object instead of using the new operator.
Int [ ] scores = {12, 13, 14, 15} (this array would have a size of 4)
An entire array can be passed as a parameter, making the formal parameter an alias of the original.
(**** NEED EXAMPLE **)
Instantiating and array of objects reserves…
room to store references only. The objects that are stored in each element much be instantiated separately.
Command-line arguments are stored in…
an array of String objects and are passed to the main method.
Example public static void main(String[ ] args)
Command line accessed through args [0]
A java method can be defined to accept a varying number of parameters.
Example public void test(int count, String name, double . . . nums)
Using an array with more than two dimensions …
is rare in an object-oriented system.