Thursday, April 23, 2015

Java Arrays

An array is an object used to contain and organize data of the same data type, including objects (other arrays also) and primitive values. The values or elements in the array are located at indexes or subscripts, starting at position 0 and ending at the declared array size position.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.

 Sorting the elements in an array can be done through two different methods, selection sort and insertion sort. Selection sorting is accomplished using two for loops to pick an initial position, scanning each position in the array, selecting the correct value which should go there, and then repeating this forever position in the array. Insertion sorting is accomplished by shifting a value into its correct position into a "correct side" of the array which is created from only "verified" values


When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known