Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods for arrays 4. Declaring and using arrays 5. Example code 6. Practice problems 7. Answers
Arrays An array is an object which are dynamically created. An array can hold values, and these values, or elements, are stored at a specific location known as the index.
Terms 1.Index 2.Element 3.Array length 4.Logical size 5.Physical size
Index Arrays are made of a grouping of values that are in a specific order, this order starts at 0 and each succeeding value increases by one integer value meaning that the final value will have a location of n-1 where n is equal to the size of the array. The specific location of each value is called the index
Elements Elements are the values that are stored in each index. For instance, an element can be the integer 1, the string “Hello World”, the character ‘a’, or any other value type.
Array Length The array length is the number of indices in the specified array. This concept can be thought of in a couple ways. The length can be the index number of the last array plus one. This can be useful when you are given just the index number of the last array. Or you can simply use the length() method which will return the length of the array. This can be very useful when you are using loops and want to stop at the end of the array.
Logical Size The logical size of an array is the number of indices in the array that are being used. Therefore, if array1[] has memory allocated to store 5 values and it only contains 2 values, it has a logical size of 2. This is analogous to the egg carton below. While it has room for many eggs, it is only using a limited amount of holes.
Physical Size The physical size is the total number of values that an array is able to hold. This means that even if array1[] has memory allocated for 5 values, but it is empty, its physical size is 5. This is analogous to the egg carton below because even though it has room for many eggs, it doesn’t have to fill them to be able to hold 12 eggs
One Dimensional Arrays A one dimensional array is a single array in which each element can be located in a one dimensional location. A one dimensional array can be compared to graphing on just the X axis.
Two Dimensional Arrays A two dimensional array can be thought of as an array of an array. Elements of a two dimensional array can be located by using rows and columns. A two dimensional array can be compared to graphing using the X axis and Y axis. The syntax for declaration and allocation for a two dimensional array is int[][] a2 = new int[10][5];
Ragged Arrays Arrays can have different numbers of elements in a row. These arrays are called ragged arrays
Parallel Arrays Parallel arrays are one dimensional arrays that have elements that correspond to elements in the other array in the same index. An example of this would be a phone directory in which a name corresponds to a phone number
Methods for Arrays Click here to view methods that can be used with arrays
Declaring Arrays In order to use an array you must first declare the variable that will be used to refer to the array. The syntax for declaring a one dimensional array is int[] array1; After that, you must create the array, the syntax for that is array1 = new int[10]; This also allocates memory for the array.
Example Code Following this slide is an example of how arrays can switch contents. This code demonstrates the declaration of arrays, allocating memory for each array, and setting values to indices of the array.
Example Code public class ArraySwitch { public static void main(String[] args) { int[] array1; int[] array2; int[] temp; array1 = new int[3]; array2 = new int[3]; temp = new int[3]; array1[0] = 0; array1[1] = 1; array1[2] = 2; array2[0] = 10; array2[1] = 11; array2[2] = 12; temp[0] = array1[0]; temp[1] = array1[1]; temp[2] = array1[2]; array1[0] = array2[0]; array1[1] = array2[1]; array1[2] = array2[2]; array2[0] = temp[0]; array2[1] = temp[1]; array2[2] = temp[2]; }
int[] array1; int[] array2; int[] temp; array1 = new int[3]; array2 = new int[3]; temp = new int[3]; This is the part of the code that first declares the arrays and then allocates memory for each of the arrays. array1[] and array2[] are the two arrays that will swap one another’s elements, temp[] is the array that will temporarily hold the elements of array[]1 while array1[] is receiving the elements from array2[]. Since each array will need to hold 3 integer values the code sets limit of the capacity of the arrays to 3.
array1[0] = 0; array1[1] = 1; array1[2] = 2; array2[0] = 10; array2[1] = 11; array2[2] = 12; This code sets values to each of the indices of array1[] and array2[]. Remember that when using arrays, you start counting at 0, so the arrays we are using which hold three values will have a maximum index number of 2.
temp[0] = array1[0]; temp[1] = array1[1]; temp[2] = array1[2]; array1[0] = array2[0]; array1[1] = array2[1]; array1[2] = array2[2]; array2[0] = temp[0]; array2[1] = temp[1]; array2[2] = temp[2]; This code is the code responsible for swapping the values of array1[] with array2[]. Even though each array can only hold a maximum of three values, they are able to change those values after they have been assigned.
Question One Which of the following methods would be used to display the contents of an array. The answer choices will be on the next three slides.
Answer A public static void dispArray(int[] arrays) { int i = 0; for(; i < 3; i++){ System.out.print(arrays[i]); }
Answer B public static int dispArray(int[] arrays) { int i = 0; for(; i < 3; i++){ return arrays; }
Answer C public static void dispArray(int[] arrays) { int i = 0; for(; i < 4; i++){ System.out.print(arrays[i]); }
Answer One The correct answer is A. B is not correct because arrays[] is not compatible with the integer type method. C is not correct because it tries to display the element of an index that is not defined for the array
Question Two After the following code has been run, what is true? int array1[] = new int[10]; A.array1[0] is undefined B.array1[10] is undefined C.the code will not compile D.array1[6] = 0
Answer Two The correct answer is B and D B is correct because since the array has 10 indices and it starts with 0 array1[10] is an undefined location. D is correct because 0 is the default value for an int value
Code Problem When the people in charge of making the school directories for Upper Dublin, Java, and Silicon Valley they accidentally entered the phone numbers for Java into the Upper Dublin Directory, the Upper Dublin phone numbers into the Silicon Valley directory and the Silicon Valley phone numbers into the Java directory
UD –Alex –Kevin –Ben –Matt –Andrew SV Chip IC Sandy Bill Alan Java Ari Eva Charles Byron James
Write a program that creates and displays three parallel arrays, one for each district (After the corrections have been made). Make a method called arraySwap() that will swap arrays’ contents in order to put the correct information in the correct array.