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

Post on 12-Jan-2016

215 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Arrays(Part 1)

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

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

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?

Can we store information in a list

or something?

We can…

using ARRAYS.

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.

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.

Accessing Information

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

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

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

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]

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?

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?

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

Declaring & Using Arrays

• In Java, arrays are objects.

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

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.

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.

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

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 + “ “); } }

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

Problem 9

• What is the output?

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

Problem 10

• What is the output?

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

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)

top related