chapter eight: arrays

29
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

Upload: indira-chan

Post on 03-Jan-2016

86 views

Category:

Documents


1 download

DESCRIPTION

Chapter Eight: Arrays. Terms and what they mean 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter Eight: Arrays

Chapter Eight: Arrays1. 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

Page 2: Chapter Eight: Arrays

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.

Page 3: Chapter Eight: Arrays

Terms

1. Index

2. Element

3. Array length

4. Logical size

5. Physical size

Page 4: Chapter Eight: Arrays

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

Page 5: Chapter Eight: Arrays

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.

Page 6: Chapter Eight: Arrays

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.

Page 7: Chapter Eight: Arrays

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.

Page 8: Chapter Eight: Arrays

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

Page 9: Chapter Eight: Arrays

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.

Page 10: Chapter Eight: Arrays

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

Page 11: Chapter Eight: Arrays

Ragged Arrays

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

Page 12: Chapter Eight: 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

Page 13: Chapter Eight: Arrays

Methods for Arrays

Click here to view methods that can be used with arrays

Page 14: Chapter Eight: 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.

Page 15: Chapter Eight: Arrays

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.

Page 16: Chapter Eight: Arrays

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

• }

Page 17: Chapter Eight: Arrays

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.

Page 18: Chapter Eight: Arrays

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.

Page 19: Chapter Eight: Arrays

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.

Page 20: Chapter Eight: Arrays

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.

Page 21: Chapter Eight: Arrays

Answer A

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

Page 22: Chapter Eight: Arrays

Answer B

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

Page 23: Chapter Eight: Arrays

Answer C

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

Page 24: Chapter Eight: Arrays

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

Page 25: Chapter Eight: Arrays

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

Page 26: Chapter Eight: Arrays

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

Page 27: Chapter Eight: Arrays

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

Page 28: Chapter Eight: Arrays

• UD

– Alex 789-456-4566

– Kevin 789-546-1230

– Ben 789-305-7566

– Matt 789-310-7831

– Andrew 789-000-1236

• SV

• Chip 215-628-1311

• IC 215-641-4899

• Sandy 215-836-2939

• Bill 215-836-4569

• Alan 215-836-9784

• Java• Ari 123-549-8135

• Eva 123-048-3540

• Charles 123-105-6000

• Byron 123-565-1506

• James 123-007-0156

Page 29: Chapter Eight: Arrays

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