Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

25
Arrays CS177 (Week 06)

Transcript of Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Page 1: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Arrays

CS177

(Week 06)

Page 2: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Announcements

● Project 2 due today● Project 3 will be posted tomorrow

Page 3: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Questions?

Page 4: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Definitions

● Homogeneous– If you have an array A, then A[0] = 0 and A[1] =

“string” is not valid

● Static– Size has to be specified when creating the array

Page 5: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Creating an array

● int[] age;– Creates a variable 'age' which can be used to

reference an array of int type. No data can be stored in it

● age = new int[10];– Now this variable can be used to store data. This

array can have 10 int values.

● These two instructions are equivalent to● int[] age = new int[10];

Page 6: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Accessing data in an array

● To store a value in an array at some position, do : age[position] = value;– So, if you want the first value in the array 'age' as '1',

then do : age[0] = 1;

● Note args was an array of String type.● Same as args, if you want to access a value in

'age', you write age[position]● Remember that in java most of the time

numbering starts with 0. i.e. the first value in 'age' is age[0] and not age[1]

Page 7: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

cont...

● This is one way of initializing and assigning values to an array:

● String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

● In this example, days is an array of size 7.

Page 8: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Length of array

● To get the length of String str, we used str.length()

● To get length of an array vector, we use vector.length

● Note, there are no parentheses after length

Page 9: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

default value

● When you declare an array of type int, double, boolean or char, java assigns a default value to all the elements of the array.

● The default value is 0 for int and double, false for boolean and '\0' for char

● For others (like String), you have to explicitly assign some value

Page 10: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

loops on arrays

● One of the most used combinations● An example for summing all arguments

provided by user (java Sum 2 3 4 5 12 3)● int sum = 0;● for(int i = 0; i < args.length; i++)● sum += Integer.parseInt(args[i]);● Note that its a standard practice that you use

array.length even if you know the length of array.

Page 11: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Swapping values

● Suppose you have 2 variables int a and int b. And you want to swap their values. Below is a sample code:

● int a = 5;● int b = 6;● int temp;● temp = a; //temp = 5● a = b; //a = 6● b = temp; //b = 5

Page 12: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Swapping values

● Similarly, in case of arrays, if we want to swap element i and j, it will be :

● int temp;● temp = vector[i];● vector[i] = vector[j];● vector[j] = temp;

Page 13: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

2D arrays

● Suppose you want to store coordinates of a car at 10 different times. How do we do that?

● We use 2 dimensional arrays● int[][] coordinates;● coordinates = new int[10][2];● Or, equivalently, int[] coordinates = new int[10]

[2];

Page 14: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Sample 2D array

● Below is a code to take data from user to fill the coordinates array;

● for(int i = 0; i < coordinates.length; i++) {● coordinates[i][0] = StdIn.readInt();● coordinates[i][1] = StdIn.readInt();● }

Page 15: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Another example from lecture

int[][] table = new int[5][10];

int label = 1;

for(int i = 0; i < 5; i++)

for(int j = 0; j < 10; j++) {

table[i][j] = label;

label++;

}● What is the value of table[i][j]? (i * 10 + j + 1)

Page 16: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

cont...

● Let us go though the loop sequentially:

● Initially, i = 0 and j = 0 and label = 1 then table[0][0] = 1

● Then i = 0, j = 1; label = 2, so table[0][1] = 2

● So on, then i = 0; j = 9; label = 10, so table[0][1] = 10

● Then i = 1, j = 0, label = 11, so table[1][0] = 11;

● So on, then i = 1 = 1, j = 9, label = 20, so table[1][9] = 20

Page 17: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

cont..● What happens when we swap the two for

loops:● for(int j = 0; j < 10; j++)● for(int i = 0; i < 5; i++) {● table[i][j] = label;● label++;● }● table[i][j] = j * 5 + i = 1

Page 18: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Liar Liar

● There is nothing called multidimensional arrays

● Its actually arrays of arrays (of arrays ....)● Think of int[] as some datatype say intA, then

int[][] is actually intA[], i.e. Array of data type int[]

● So, actually, you can decide size of one dimension and then separately decide size of other dimension

Page 19: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Ragged arrays

● Example:● int[][] pharma = new int[5][];● for(int i = 0; i < 5; i++)● pharma[i] = new int[i+1];

Page 20: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

cont....

● Result:

Page 21: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Use of ragged arrays

● Suppose you want to store courses students are doing.

● Fixed number of students, but each student can be doing different number of courses

● Ragged arrays save space.

Page 22: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

>2 dimensions?

● Example of 3D array:● int[][][] rubiksCube = new int[3][3][3];● int count = 1;● for(int i = 0; i < rubiksCube.length; i++)● for(int j = 0; j < rubiksCube[i].length; j++)● for(int k = 0; k < rubiksCube[i][j].length; k+

+)● rubiksCube[i][j][k] = count++;

Page 23: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

cont..

● Notice rubriksCube[i][j].length and rubriksCube[i].length

● As we discussed earlier, multidimensional arrays are arrays of arrays, so rubriksCube[i] is an array and so is rubriksCube[i][j]

● Is rubriksCube[i][j][k] an array?

Page 24: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

(dis)advantages of multidimensional arrays

● Sometimes, having higher dimensions are troublesome

● In general, with an addition of a dimension, you include another loop. 5 dimension means 5 loops. 5 loops means messy code

● Linus Torvalds (The father of awesome Linux) says: If you are using more than 3 loops, you are doing it wrong.

● But sometimes you do need more loops.

● How many elements are there int[5][10[100]?

Page 25: Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.

Questions