Download presentation
Presentation is loading. Please wait.
Published byPaul Burke Modified over 8 years ago
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 dataname of the array variable int[ ] array1 = new int[10]; reserved word newsize 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 0position 4position 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
Assigning 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
Assigning 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
Assigning 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] = (int)(Math.random()*100); Does array1[10] exist?
15
Assigning 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
Assigning Arrays int[ ] array1 = new int[10]; Assigning: for(int k = 0; k < 10; k++) array1[k] = (int)(Math.random()*100); Printing: for(int k = 0; k < 10; k++) 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: for(int k = 0; k < array1.length; k++) 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; for(int k = 0; k < name3.length; k++) 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 integer array of size 10. Assign random values between 0-100 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 0-100 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; for(int k = 0; k < 10; k++) 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; for(int k = 0; k < array1.length; k++) System.out.print(array1[k] + “ “); Output:0 1 2 3 4 5 6 7 8 9
24
What is the output? int[ ] array1 = new int[10]; for(int k = 0; k < array1.length; k++) array1[k] = array[k+1]; for(int k = 0; k < array1.length; k++) 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:0 0 0 0 0 5 6 7 8 9
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:1 4 9 16 25 36 49 64 81 0
27
Exercise 4 Declare two integer arrays, each of size 20. Initialize the first array with a value of 10. Now initialize the second array with the values of the first array times 100 int[ ] array1 = new int[20]; int[ ] array2 = new int[20];
28
Exercise 5 Declare an integer array of size 10. Set random values between 0-100 to this array. Print out this array. Declare an integer array of size 15. Set random values between 100- 500 to this array. Print out this array.
29
Exercise 6 Mirror Arrays: Create an array of size 20 and assign random values between 0-100 to each element. Print out this array. Now create another array of size 20 whose values will duplicate this array. Print out this array as well.
30
Exercise 7 Declare two integer arrays the size of 10. Generate a random number between 0-100 for the first array. If this number is greater than or equal 50, generate 0 for the second array. If the number is smaller than 50, generate 1 for the second array. Do this 10 times to fill up both arrays. Display both arrays.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.