Chapter 7: Arrays (Java: Introduction to Problem Solving and Programming)
array
A collection of items of the same type. It is like declaring a bunch of variables at the same time in a compact way.
instance variable length
An array is an object. The public instance variable that an array has is length. When you call upon the length you get the index. For example, int [ ] array = new int [9] array.length == 9.
indexed variables or elements
An example of an indexed variable or element includes temperature[0]. An element also can be used to refer to an indexed variable or the value of an index variable.
index variables as method arguments
An indexed variable can be used anywhere that you can use any other variable of the base type of an array. A method can change the state of the an object but cannot replace the object with another one.
selection sort
In this algorithm, we want to rearrange the values of the array so that array [0] is the smallest value and array [1] is the next smallest value. for (index = 0; index<a.length; index++){ place the index+1^th smallest element in a[index] }
last element in an array
Since the index of the first element in an array is always zero, the last index number is not the length of the array. It is (.length - 1) For example, if int [ ] array = new int [9] array.length == 9. the last element of the array is array.length-1 == array[9-1] == array [8] == last element. If an index expression evaluates to some integer other than 0 through one less than the length of the array, the index is out of bounds or invalid.
index
The integer expression within the square brackets of an element is an index. With arrays, the numbering within the brackets starts with 0 not 1. For example, int [ ] array = new int [9] == array [0], array[1], array[2], array [3], array[4], array[5], array[6], array[7] , array[8] There are nine elements here, but the numbering starts at 0 and ends at 8 instead of 1 to 9. But, there are 9 elements within the array. There is no array[9] variable.
length
The number of elements in an array is called the length. It is the index.
arguments for method main
The parameter declaration in public static void main(String[] args) indicates that args in is an array whose base type is string. Main is invoked automatically and given default array of strings as a default argument. The strings will automatically be made elements of the array args. Can pass an array of strings to main as an argument.
== and =
These two operators behave the same way they would if you were dealing with a variable.
sequential search
a sequential search looks for an array element from beginning to end
multidimensional arrays
int [] [] table; table = new int [10] [6] has 6 rows across and 10 rows down and a total of 60 elements.
searching an array
to search for a certain item in array