Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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.

3 Terms 1.Index 2.Element 3.Array length 4.Logical size 5.Physical size

4 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

5 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.

6 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.

7 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.

8 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

9 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.

10 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];

11 Ragged Arrays Arrays can have different numbers of elements in a row. These arrays are called ragged arrays

12 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

13 Methods for Arrays Click here to view methods that can be used with arrays

14 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.

15 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.

16 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]; }

17 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.

18 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.

19 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.

20 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.

21 Answer A public static void dispArray(int[] arrays) { int i = 0; for(; i < 3; i++){ System.out.print(arrays[i]); }

22 Answer B public static int dispArray(int[] arrays) { int i = 0; for(; i < 3; i++){ return arrays; }

23 Answer C public static void dispArray(int[] arrays) { int i = 0; for(; i < 4; i++){ System.out.print(arrays[i]); }

24 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

25 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

26 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

27 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

28 UD –Alex789-456-4566 –Kevin789-546-1230 –Ben789-305-7566 –Matt789-310-7831 –Andrew 789-000-1236 SV Chip215-628-1311 IC215-641-4899 Sandy215-836-2939 Bill215-836-4569 Alan215-836-9784 Java Ari123-549-8135 Eva123-048-3540 Charles123-105-6000 Byron123-565-1506 James123-007-0156

29 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.


Download ppt "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."

Similar presentations


Ads by Google