An array is a collection of values of the same data type. We store/access elements from the array by using the subscripts. Subscripts represent the positions in the array where storing and accessing can be performed. The subscripts start at 0, and the last position is size-1. The reason why is size – 1 is because we start counting at 0.
Traditional: use the following formula to define an array of any data type
<data type> [ ] <id> = new <data type>[<int–representing size>];
<data type>: can be any 8 primitive data type or Object, e.g., String. Notice that the data type on the left side of the = sign must match the data type on the right side of the = sign.
[]: is the identifier that you will handle an array
<id>: is the name of the array, use camel case style
[int]: this will represent the total size of your array
Shortcut: When you know the elements that you would like to store in an array, you can use the following format:
<data type> [ ] <id> = {e1, e2, e3, …, en};
Where e1 is the first element to store, e2 is the second element to store, and so on. Notice that all the elements must be the same data type.
For example, the following theArray holds 5 ints:
numbers held 3 1 8 6 7
Array indexes 0 1 2 3 4
When you know the exact position of the element you want to access, you must specify the position inside the [] of the array:
theArray[pos], where pos is an integer between 0 – n-1
Printing the elements of the array is not as trivial as it sounds. For example, try to print the array theArray as follows:
System.out.println( theArray );
What do you expect?
A line of numbers separated by a comma? E.g., 4,18,86,42,9,222
A line of numbers separated by space? E.g., 4 18 86 42 9 222
A concatenation of all the numbers? E.g., 41886429222
Error?
Something else?
But instead, you got something like:
By printing, we obtained an output similar to the following:
[I@42242ea4
Representing the hexadecimal memory address location where the array is located. It means that the memory is holding an array ([) of integers (I) that is located at (@) memory location (42242ea4). Meanwhile, to process the elements that are inside the array, we will need to do it independently. For example, by printing all the five elements of the array, we need to:
System.out.println(array [0]);
System.out.println(array [1]);
System.out.println(array [2]);
System.out.println(array [3]);
System.out.println(array [4]);
These statements will print the values stored from positions 0 – 4, i.e., 3, 1, 8, 6, 7
By using the shortcut, you can also represent the statements in the following:
int [] theArray = {3, 1, 8, 6, 7};
and by using the power of looping, we can simplify our previous code with the following loop:
for(int i = 0; i < 5; i++)
System.out.println(theArray [i]);
Suppose that someone forgot to provide you with two more elements to store in the array. Using the shortcut is very easy; you can simply add the two elements at the end of the array as follows:
int [] theArray = {3, 1, 8, 6, 7, 13, -1};
However, that will cause you to modify your code of printing to:
for(int i = 0; i < 7; i++)
System.out.println(theArray [i]);
This was easy; however, we cannot afford to keep modifying our code that prints the elements every time the user changes the size of the array. We need an identifier that tells us how big is the size of the array. This is called the length of the array.
The array has an immutable field called length. The length is an integer representing the total capacity of the array. In our previous example, the capacity is 5, and the way to extract that value is by calling the length of the array a, as follows: a.length.
It is called immutable because once you define an array with a specific size, you cannot change it.
So in our example, you can fix the problem of keep adding and modifying the code by using the following
for(int i = 0; i < theArray.length; i++)
System.out.println(theArray [i]);
Notice that the accessible positions of the array are from 0 to a.length-1, and therefore, you need to be very careful in accessing elements that belong in that specific range. If you go “out of the bounds” of the array, Java will complain through something called:
java.lang.ArrayIndexOutOfBoundsException
For example, if we attempt to print:
System.out.println(theArray [-1]);
Or in the original array that contains five elements, the following:
System.out.println(theArray [5]);
Both will produce the ArrayIndexOutOfBoundsException. Checking the boundaries of the array is critical to avoid what is considered to be a buffer underflow/buffer overflow.
AP CS A
[Using Methods]
MOD-2.C.3 A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
MOD-2.C.4 A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
MOD-2.C.5 Programmers write method code to satisfy the postconditions when preconditions are met.
[Documentation Comments]
MOD-2.C.3 A precondition is a condition that must be true just prior to the execution of a section of program code in order for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied.
MOD-2.C.4 A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object.
MOD-2.C.5 Programmers write method code to satisfy the postconditions when preconditions are met.
ACM CCECC
[Using Methods]
SDF-14. Analyze programming code that utilizes preconditions, postconditions, and invariants.
Explain the importance of algorithms in the problem-solving process.
Demonstrate how a problem may be solved by multiple algorithms, each with different properties.
Read a given program, and explain what it does
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Reading and explaining code
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.
Apply basic programming style guidelines to aid readability of programs such as comments, indentation, proper naming of variables, etc.
Basic testing (perhaps using suitable frameworks) including test case design
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.
Key modularity constructs such as functions (and methods and classes, if supported in the language) and related concepts like parameter passing, scope, abstraction, data encapsulation, etc.
When and how to use standard data structures
Structured data types available in the chosen programming language like sequences (e.g., arrays, lists), associative containers (e.g., dictionaries, maps), others (e.g., sets, tuples) and when and how to use them.