arrays (part 1) computer science erwin high school fall 2014

38
Arrays (Part 1) Computer Science Erwin High School Fall 2014

Upload: dinah-french

Post on 12-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Arrays(Part 1)

Computer ScienceErwin High School

Fall 2014

Page 2: Arrays (Part 1) Computer Science Erwin High School Fall 2014

How to Use the Slides

• As you go through the slides, answer the questions that appear on a separate piece of paper.

• You will turn this paper in when you complete the slideshow.

• If you have questions as you read, list these on your paper as well.

Page 3: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Something to Think About

• We have learned a lot of useful tools in our coding practice:

– Using variables to store a piece of information: numbers, text, truth values (true or false), etc.

– Conditional statements to only run code in certain situations (if-else statements)

– Loops to run blocks of code multiple times or if certain conditions are met (while, do-while, for loops, etc.)

Page 4: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Something to Think About

• But what if we want to store a lot of information?

• What if we wanted a list of all the student id numbers at our school? Or a list of all the student names? Or phone numbers? Or addresses? Or GPAs?

Page 5: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Can we store information in a list

or something?

Page 6: Arrays (Part 1) Computer Science Erwin High School Fall 2014

We can…

using ARRAYS.

Page 7: Arrays (Part 1) Computer Science Erwin High School Fall 2014

What is an Array?

• An array is a simple but powerful programming language construct used to group and organize data.

• It is a list of values, where each value is stored at a specific numbered position in the array.

Page 8: Arrays (Part 1) Computer Science Erwin High School Fall 2014

What is an Array?

• The number corresponding to each position is called an index.

• Array indexes always begin at zero, just like indexes for the characters that make up a String.

Page 9: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Accessing Information

• To access a value stored in an array, we use the name of the array followed by the index in square brackets.

Page 10: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Array Example

• Suppose we wanted to store the heights of 11 different students in a class.

• We could create an array of integer values to store the heights of the student (in inches).

Page 11: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Array Example

69

61

70

74

62

69

66

73

79

62

70

height

0

1

2

3

4

5

6

7

8

9

10

Page 12: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Array Example

69

61

70

74

62

69

66

73

79

62

70

height

0

1

2

3

4

5

6

7

8

9

10

index

Value of height[5]

Page 13: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 1

69

61

70

74

62

69

66

73

79

62

70

height

0

1

2

3

4

5

6

7

8

9

10

• Consider the following code segment:

int x = height[8]; System.out.println(x);

• What is the output?

Page 14: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 2

69

61

70

74

62

69

66

73

79

62

70

height

0

1

2

3

4

5

6

7

8

9

10

• Consider the following code segment:

int x = 6; System.out.println(height[x]);

• What is the output?

Page 15: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 3

69

61

70

74

62

69

66

73

79

62

70

height

0

1

2

3

4

5

6

7

8

9

10

• What are each of the following?

– a. height[2] + height[5]

– b. height[2 + 5]

– c. height.length

Page 16: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Declaring & Using Arrays

• In Java, arrays are objects.

• To create an array, we must declare an array just like other objects.

Page 17: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Array Declaration

• The array is instantiated using the new operator, which allocates (or sets aside) memory space to store the values.

• The following code represents the declaration for the student height array (for 11 students).

int[] height = new int[11];Notice the braces after int. This tells the computer we are creating an array of integers and naming the array height.

Page 18: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Array Declaration

• The array is instantiated using the new operator, which allocates (or sets aside) memory space to store the values.

• The following code represents the declaration for the student height array (for 11 students).

int[] height = new int[11];

The 11 inside the second set of braces is to tell the computer how many values will be stored in the array.

Page 19: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Array Elements

• A value stored in an array is sometimes called an array element.

• The type of values that an array holds is called the element type of the array.

• Arrays can hold primitive types (int, double, boolean, etc.) or objects from any other class (String, Ball, Card, Student, etc.)

Page 20: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

Page 21: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

An array of integers is created and named list.

Page 22: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

It will hold 15 total values.

Page 23: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

It will hold 15 total values.0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page 24: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

For each index value from 0 to 14 (index < 15) the array element at that index will contain the value of index * 10.

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page 25: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

For each index value from 0 to 14 (index < 15) the array element at that index will contain the value of index * 10.

0

10

20

30

40

50

60

70

80

90

100

110

120

130

140

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page 26: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

The element at index 5 is then set equal to 999.

0

10

20

30

40

50

60

70

80

90

100

110

120

130

140

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page 27: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

The element at index 5 is then set equal to 999.

0

10

20

30

40

999

60

70

80

90

100

110

120

130

140

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page 28: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

The for loop here is structured different from ones we have seen before. This is specifically for an array of values or objects.

0

10

20

30

40

999

60

70

80

90

100

110

120

130

140

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page 29: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

For each integer element in list this loop will print out the number. The number is stored in the variable value each time through the loop. As soon as the end of the list is reached, the loop stops.

0

10

20

30

40

999

60

70

80

90

100

110

120

130

140

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Page 30: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Another Array Example public class BasicArray { public static void main(String[] args) { int[] list = new int[15];

for (int index = 0; index < 15; index++) list[index] = index * 10;

list[5] = 999;

for (int value: list) System.out.print(value + “ “); } }

010

20304099960708090100110120130140

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Output: 0 10 20 30 40 999 60 70 80 90 100 110 120 130 140

Page 31: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 4

• What is the output?

int[] nums = {2, 3, 5, 1, 0, 6, 7};

System.out.println(nums[0]); System.out.println(nums[2]); System.out.println(nums[5]);

Page 32: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 5

• What is the output?

int[] nums = {2, 3, 5, 1, 0, 6, 7};

System.out.println(nums[1+3]); System.out.println(nums[7/2]); System.out.println(nums[6]);

Page 33: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 6

• What is the output?

double[] nums = new double[10];

nums[0] = 10.5; nums[3] = 98.6; nums[2] = 77.5;

System.out.println(nums[0]); System.out.println(nums[3]); System.out.println(nums[7]);

Page 34: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 7

• What is the output?

String[] words = new String[10];

words[0] = “dog”; words[3] = “cat”; words[2] = “pig”;

System.out.println(words[0]); System.out.println(words[3]); System.out.println(words[7]);

Page 35: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 8

• What is the output?

int[] nums = {3, 2, 5, 1, 0, 6} for (int loc = 0; loc < nums.length; loc++) { System.out.println(nums[loc]); }

Page 36: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 9

• What is the output?

int[] nums = {3, 2, 5, 1, 0, 6} for (int item: nums) { System.out.println(item); }

Page 37: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Problem 10

• What is the output?

String[] words = {“cat”, “pig”, “dog”} for (String animalName: words) { System.out.println(animalName); }

Page 38: Arrays (Part 1) Computer Science Erwin High School Fall 2014

Program 1

• Create a class file called PlayingCard.

• It should have two properties – a String to hold the suit and an int to hold the value.

• It should also have five methods:– getSuit, setSuit, getValue, setValue and

toString (that prints out the suit and value)