Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Arrays (Part 1) Computer Science Erwin High School Fall 2014."— Presentation transcript:

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

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

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

4 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?

5 Can we store information in a list or something?

6 We can… using ARRAYS.

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

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

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

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

11 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

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

13 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?

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

15 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

16 Declaring & Using Arrays In Java, arrays are objects. To create an array, we must declare an array just like other objects.

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

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

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

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

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

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

23 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

24 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

25 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 10 0 11 0 12 0 13 0 14 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

26 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 10 0 11 0 12 0 13 0 14 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

27 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 99 9 60 70 80 90 10 0 11 0 12 0 13 0 14 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

28 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 99 9 60 70 80 90 10 0 11 0 12 0 13 0 14 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

29 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 99 9 60 70 80 90 10 0 11 0 12 0 13 0 14 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

30 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 + “ “); } 0 10 20 30 40 99 9 60 70 80 90 10 0 11 0 12 0 13 0 14 0 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

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

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

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

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

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

36 Problem 9 What is the output? int[] nums = {3, 2, 5, 1, 0, 6} for (int item: nums) { System.out.println(item); }

37 Problem 10 What is the output? String[] words = {“cat”, “pig”, “dog”} for (String animalName: words) { System.out.println(animalName); }

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


Download ppt "Arrays (Part 1) Computer Science Erwin High School Fall 2014."

Similar presentations


Ads by Google