Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store...

21
Java Programming Arrays Chapter 8

Transcript of Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store...

Page 1: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Java ProgrammingArrays

Chapter 8

Page 2: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Introduction to Arrays

What if we need to store test scores for all students in our class.

We could store each test score as a unique variable:

int score1 = 85;

int score2 = 92;

int score3 = 90;

int score4 = 74;

int score5 = 83;

Or we could store the values in an array

Test scores

8592907483

Page 3: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Introduction to Arrays An array is a data structure used to process

a collection of data that is all of the same type An array behaves like a numbered list of

variables with a uniform naming mechanism It has a part that does not change: the name of

the array It has a part that can change: an integer in

square brackets For example, given five scores:

score[0], score[1], score[2], score[3], score[4]

Page 4: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Creating Arrays An array that behaves like this collection of

variables, all of type integer, can be created using one statement as follows:int[] score = new int[5];

Or using two statements:int[] score;score = new int[5]; The first statement declares the variable score to be of the array

type int[] The second statement creates an array with five numbered

variables of type int and makes the variable score a name for the array

Page 5: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Creating Arrays

The individual variables that together make up the array are called indexed variables They can also be called subscripted variables or

elements of the array The number in square brackets is called an

index or subscript In Java, indices must be numbered starting with 0, and nothing else

score[0], score[1], score[2], score[3], score[4]

Page 6: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Creating Arrays

The number of indexed variables in an array is called the length or size of the array

When an array is created, the length of the array is given in square brackets after the array type

The indexed variables are then numbered starting with 0, and ending with the integer that is one less than the length of the array

score[0], score[1], score[2], score[3], score[4]

Page 7: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Creating Arrays

Test scores array

score 85 92 90 74 83

indices 0 1 2 3 4

Page 8: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Initializing Arrays

An array can be initialized when it is declared Values for the indexed variables are enclosed in

braces, and separated by commas The array size is automatically set to the number of

values in the braces

int[] score = {85, 92, 90, 74, 83};

Using this shorthand notation, you have to declare, create, and initialize the array all in one statement. Using the two statement method would cause a syntax error

Page 9: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Initializing Arrays Another way of initializing an array is by using a for loop:

int[] score = new int[5];int index;for (index = 0; index < score.length; index++) score[index] = 85;

If the elements of an array are not initialized explicitly, they will automatically be initialized to the default value for their base type

Page 10: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

public class Test {public static void main(String[ ] args) {

int[ ] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] +

values[4]; }}

Initializing Arrays What is created with the following code?

Page 11: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Declare array variable values, create an array, and assign its reference to values

public class Test {public static void main(String[ ] args) {

int[ ] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] +

values[4]; }}

Initializing Arrays

0

0

0

0

0

value[0]

value[1]

value[2]

value[3]

value[4]i = 1: value[1] is 1 + value[0] = 1 + 0 = 1i = 2: value[2] is 2 + value[1] = 2 + 1 = 3i = 3: value[3] is 3 + value[2] = 3 + 3 = 6i = 4: value[4] is 4 + value[3] = 4 + 6 = 10

1

3

6

10

value[0] is value[1] + value[4] = 1 + 10 = 11

11

Page 12: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Initializing Arrays

Scanner input = new Scanner(System.in);System.out.print("Enter 10 values: ");for (int i = 0; i < 10; i++) myList[i] = input.nextDouble();

How do we input arrays through a scanner?

Page 13: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Arrays of Strings

Strings can be part of an array Create array of Strings with 3 elements

String[] deptName = {"Accounting", "Human Resources", "Sales"};

for(int a = 0; a < deptName.length; ++a)System.out.println(deptName[a]);

Page 14: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Printing arrays

for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " ");}

To print the entire array, create a loop to print each element

Page 15: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Summing all elements

double total = 0;for (int i = 0; i <

myList.length; i++) { total = total + myList[i];}

Page 16: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Finding the largest element

double max = myList[0];for (int i = 1; i < myList.length;

i++) { if (myList[i] > max)

max = myList[i];}

Page 17: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Searching Arrays

To search for a specific value, simply loop through the loop Look at each entry and compare it with the specified

value

boolean found = false;int i = 0;while (i < myList.length && !found) {

if (searchEntry == myList[i]) found = true;

i++; }if (found)

System.out.println(searchEntry + " occurs in array");

Page 18: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Array Parameters

Both array indexed variables and entire arrays can be used as arguments to methods An indexed variable can be an argument to a

method in exactly the same way that any variable of the array base type can be an argument

Page 19: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Array ParametersJava uses pass by value to pass arguments to a method. There are important differences between passing a value of variables of primitive data types and passing arrays.

For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method.

For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument.

Page 20: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

public class Test { public static void main(String[] args) { int[] scores = {85, 92, 90, 74, 83};

myMethod(scores);System.out.println("scores[0] is " + scores[0]);

}

public static void myMethod(int[] numbers) {numbers[0] = 5555; // Assign a new value to

numbers[0] }}

Array Parameters

Page 21: Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

public static int[ ] reverse(int[ ] list) {int[ ] result = new int[list.length];for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {

result[j] = list[i];}return result;

}

Array Parameters

Here is a method that returns the array in reverse order