1 / 25

Arrays

Arrays. CS177 (Week 06). Announcements. Project 2 due today Project 3 will be posted tomorrow. Questions?. 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. Creating an array.

cai
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays CS177 (Week 06)

  2. Announcements Project 2 due today Project 3will be posted tomorrow

  3. Questions?

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

  5. 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];

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

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

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

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

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

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

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

  13. 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];

  14. 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(); }

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

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

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

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

  19. Ragged arrays Example: int[][] pharma = new int[5][]; for(int i = 0; i < 5; i++) pharma[i] = new int[i+1];

  20. cont.... Result:

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

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

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

  24. (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]?

  25. Questions

More Related