Вы находитесь на странице: 1из 3

Arrays This is essentially a container which holds a group of variables of the same data type.

An array is an object and so it is not a primitive type but a reference type. The array name refers to a memory location where the actual array object is stored. Each item stored in an array is called an element; the number of elements contained in a single array is set when the array is created. To create the array we must use the new keyword: int [] my_arr = new int [10] This line creates a new array of size 10. i.e a maximum of ten elements can be stored in the array. In java all array index begin at 0 i.e the first element in an array called my_arr will be stored at position 0 in the array and is referenced by both the array name and the element's index position in the array. Hence the first item is found at my_arr[0]. Notice the same square brackets we use in the declaration and initialization of the array is used to reference a particular element in the array. Since the index begins at 0 it will end at one less than the size of the array. An array which can hold a maximum of 10 elements will have index positions 0 through 9 i.e 'size of array' - 1 2 0 15 12 98 5 1 2 3 4 66 31 9 5 6 7 1 8 27 9

In the above diagram the first row of numbers represents the actual elements stored in the array, while the second row of numbers are the index values associated with each element. This index is used to access or store an element in the array. To store 98 at position 3: my_arr[3] = 98 In java when an array is created each element is initialized to a default value: 0 for any numeric arrays null - for an object array false for a boolean array In order to achieve any task on an array some form of traversal must be done. i.e. visiting each index position in the array. Since this is a repetitive task arrays are almost always processed in a loop structure. For manipulations which affect the entire array a for-loop is usually used since we can easily determine the length of the array. Arrays have a special method called 'length' which is used to return the number of elements in the array. This allows us at any point in our code to 'know' how many index positions exist in the array and may be visited. NB 'length' returns the number of elements in the array or 'size' of the array hence we must remember that we can only traverse up to position length 1! If we attempt to visit arr_name[length] java will throw an 'Array out of Bounds Exception' which, simply put, is a BAD thing! eg. //Initialize the array to store twice the value of each index position for (int i=0; i < int_array.length; i++) int_array[i] = 2 * i;

Array elements can be accessed and used as any other variable of the same type. For a numeric array

we can use the individual elements in mathematical expressions, calculations, comparisons and concatenation as any other numeric variable. Some examples are listed below: //print out the contents of the array for (int i=0; i < int_array.length; i++) System.out.println(int_array[i]); //add the element at position i to the element at position i-1 and store this sum at position i int_array [i] = int_array[i] + int_array[i-1]; //if the element at a particular position equals a given key exit the loop while (arr[pos] != key){ <code statements> } these are but a few simple examples of array manipulation and traversal. Searching an array Since arrays stores multiple data elements it is useful to determine if a particular element is in the array. For this purpose there are several searching algorithms and we will now look at two of the simplest: 1. Linear Search iterates through an array and searches each position for the desired element and returns the position at which it is found or -1 (or any other invalid index) if it is not found. 2. Binary Search this search is more efficient than the Linear search since it never searches the entire array. This search can only be performed on a sorted array since it tests whether the key is located in the upper or lower half of the array then eliminates the half in which the item cannot be found. This in effect reduces the positions to be searched in half!!! This process (eliminating half of the array) is continued until the element is found or all possible locations have been searched. We will develop this code in class. Sorting and array. Like searching, we often choose to sort our array in some order (ascending or descending) since it then makes it easier to search and is more presentable is a sorted format think of class lists, employee lists or telephone directories; How would life be if none on these were sorted alphabetically!!! We will now look at two of the simpler sorting algorithms 1. Selection Sort this algorithm is very simple and has three steps: 1. Find the smallest element in the unsorted array 2. Swap it with the element at the first position in the unsorted array 3. Repeat 1 and 2 until the entire list is sorted, starting at the second item in the array and incrementing one position each time. This method essentially breaks the array into a sorted portion and an unsorted portion until it is complete. Let us look at a simple example: (The grey indices are the sorted positions in the array.)

14 21 5 0 1 2

3 3

18 4

This is our array before we begin the selection sort algorithm.

3 0 3 0 3 0 3 0

21 5 1 5 1 5 1 5 1 2

14 18 3 4

We find that 3 is the smallest element in the array so we switch it with the contents at position 0 We find that 5 is the smallest element in the unsorted array so we switch it with the contents at position 1 We find that 14 is the smallest element in the unsorted array so we switch it with the contents at position 2

21 14 18 2 3 4

14 21 18 2 3 4

14 18 21 2 3 4

Finally we have a totally sorted array.

Вам также может понравиться