Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays.

Similar presentations


Presentation on theme: "Arrays."— Presentation transcript:

1 Arrays

2 What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3; int num4; int num5; int num6; int num7; int num8; int num9; int num10;

3 What is an array? int num1; int num2; int num3; int num4; int num5; int num6; int num7; int num8; int num9; int num10; This is very inefficient to write. What if we needed 100 elements? What about 1000?

4 What is an array? An array allows us to declare what type of data we want and how many of them we want. So if we need 10 integers, this is how we want to declare an array: int[ ] array1 = new int[10];

5 What is an array? What type of data name of the array variable int[ ] array1 = new int[10]; reserved word new size of the array

6 Decomposition of an Array
int[ ] array1 = new int[10]; The size of an array must be declared before execution. This is not legal: int[ ] array1 = new int [x] if x is undefined.

7 Decomposition of an Array
You can also assign an array in this manner. int[ ] array1 = {1, 5, 0, 6, 9, 1, 5, 3}; The size of the array above is 8 because it has 8 elements.

8 Accessing an Array Suppose I made the following declaration: int[ ] array1 = new int[10]; The size of the array above is 10. However, when I access these fields, the starting position is actually 0, meaning the final position is 9.

9 Accessing an Array int[ ] array1 = {1, 5, 0, 6, 9, 1, 5, 3}; // size is 8 position 0 position 4 position 7 What is position 1? What is position 6?

10 Accessing an Array int[ ] array1 = new int[10]; array1[0] = 56; array1[1] = 80; array1[2] = 74; array1[3] = 96; array1[4] = 71; array1[5] = 15;

11 Accessing an Array int[ ] array1 = new int[10]; // suppose all of these have values System.out.println(array[0]); int num1 = array1[7]; array1[8] = array1[4];

12 Assiging Arrays int[ ] array1 = new int[10]; So we have 10 elements. Let’s say we wanted to assign the value 100 to all elements. Are we going to write 10 of these statements: array1[0] = 100; array1[1] = 100; array1[2] = 100; array1[3] = 100; array1[4] = 100; // keeps going

13 Assiging Arrays int[ ] array1 = new int[10]; Enter the for loop for(int k = 0; k < 10; k++) array1[k] = 100; For loops are best at assigning values to arrays.

14 Assiging Arrays int[ ] array1 = new int[10]; However, you must be careful not to access elements that does not exist. for(int k = 0; k <= 10; k++) array1[k] = 100; Does array1[10] exist?

15 Assiging Arrays int[ ] array1 = new int[10]; What happens when you try to access elements that does not exist? array1[50]? Java will throw an error at you letting you know that the index of your array is out of bounds.

16 Assiging Arrays int[ ] array1 = new int[10]; Assigning: for(int k = 0; k < 10; k++) array1[k] = 100; Printing: System.out.println(array1[k] + “ “);

17 Array methods int[ ] array1 = new int[10]; array1.length will return the size of the array as an integer. Assigning: for(int k = 0; k < array1.length; k++) array1[k] = 100; Printing: System.out.println(array1[k] + “ “);

18 Review double[ ] name = new double[50]; String[ ] name2 = new String[40]; int[ ] name3 = new int[100]; for(int k = 0; k < name3.length; k++) name3[k] = 500; System.out.print(name3[k] + “ “);

19 Exercise 1 Declare an integer array of size 20. Use a for loop to initialize all values to 100. Use another for loop to print out the values.

20 Exercise 2 Declare a double array of size 10. Assign random values between to each of these elements. Print out the values of these arrays.

21 Exercise 3 Declare a double array of size 20. Assign random values between to each of these elements. Then go through the array and display its square root. Display the values of your array.

22 Review: int[ ] array1 = new int[10]; array[20] = 50; for(int k = 0; k < 10; k++) array1[k] = Math.random()*100; System.out.println(array1[k+1] + “ “);

23 What is the output? int[ ] array1 = new int[10]; for(int k = 0; k < array1.length; k++) array1[k] = k; System.out.print(array1[k] + “ “); Output:

24 What is the output? int[ ] array1 = new int[10]; for(int k = 0; k < array1.length; k++) array1[k] = array[k+1]; System.out.print(array[k] + “ “); Output: Error, array index out of bounds

25 What is the output? int[ ] array1 = new int[10]; for(int k = 5; k < array1.length; k++) array1[k] = k; for(int k = 0; k < array1.length; k++) System.out.print(array[k] + “ “); Output:

26 What is the output? int[ ] array1 = new int[10]; for(int k = 1; k < array1.length; k++) array1[k-1] = k*k; for(int k = 0; k < array1.length; k++) System.out.print(k + “ “); Output:

27 Exercise 4 Declare a double array of size 10. Assign random values between to each of these elements. Then go through the array and determine which numbers are odd. Add +1 to every odd number and flip it to even. Display the values of your array.

28 Exercise 5 Declare an array of Strings. Let’s say a size of 5 should be fine. Get input for all those strings. Ask them to input only lower case letters. Once done, change the string to all upper case and display them. Example dog DOG cat CAT mouse MOUSE You will need the string method toUpperCase(). This method returns the string value attached to it and turns everything uppercase.

29 Exercise 6 Declare a double array of size 20. Assign values between to each of them. Values should increase by 20 every array. So position 0 should be 0 and position 19 should be 380. Now display the array in reverse order. So start displaying from 380 to 0.

30 Exercise 7 Generate a 10 digit number. Separate each digit and store them in an array of 10 size long. Now, display the number in reverse order which should display the number in reverse.

31 Exercise 8 Shift Arrays exercise. Take an array of random integers between 0-10 and display this array. Now take this array and shift them by 5 cells to the left each. Note the following example. Array 1: Shift: Display your shifted array.


Download ppt "Arrays."

Similar presentations


Ads by Google