2D-Arrays Quratulain
Learning Objectives Two-dimensional arrays Declaration Initialization Applications
Introduction Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach. when created, arrays are automatically initialized with the default value of their type – String[] s = new String[100]; // default values: null – boolean[] b = new boolean[4]; // default values: false – int[] [] i = new int[10][10]; // default values: 0
Sorted array element int i, j, index, tmp; int n = arr.length; for (i = 0; i < n - 1; i++) { index = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[index]) index = j; if (index != i) { tmp = arr[i]; arr[i] = arr[index]; arr[index] = tmp; }
Introduction The first dimension represents the rows, the second dimension, the columns curly braces {} may also be used to initialize two dimensional arrays. Again they are only valid in array declaration statements. – int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} }; – you can initialize the row dimension without initializing the columns but not vice versa int[][] myArray = new int[5][]; – int[][] myArray = new int[][5]; // illegal Now initialize columns in each row – myArray[0] = new int[3]; – myArray[1] = new int[4]; – myArray[2] = new int[5];
Demonstrate a two-dimensional array // Demonstrate a two-dimensional array. class TwoD { public static void main(String args[]) { int t, i; int table[][] = new int[3][4]; for(t=0; t < 3; ++t) { for(i=0; i < 4; ++i) { table[t][i] = (t*4)+i+1; System.out.print(table[t][i] + " "); } System.out.println(); }
Home work (Magic Square) A magic square of order n is an arrangement of n 2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. Develop code using 4-rules discussed in class. Here sum is
Ragged Array The use of irregular (or ragged) multidimensional arrays is not recommended for most applications, because it runs contrary to what people expect to find when a multidimensional array is encountered. However, irregular arrays can be used effectively in some situations. – For example, if you need a very large two-dimensional array that is sparsely populated (that is, one in which not all of the elements will be used), an irregular array might be a perfect solution.
Example // Manually allocate differing size second dimensions. class Ragged { public static void main(String args[]) { int riders[][] = new int[7][]; riders[0] = new int[10]; riders[1] = new int[10]; riders[2] = new int[10]; riders[3] = new int[10]; riders[4] = new int[10]; riders[5] = new int[2]; riders[6] = new int[2]; int i, j; // fabricate some fake data for(i=0; i < 5; i++) for(j=0; j < 10; j++) riders[i][j] = i + j + 10; for(i=5; i < 7; i++) for(j=0; j < 2; j++) riders[i][j] = i + j + 10; System.out.println("Riders per trip during the week:"); for(i=0; i < 5; i++) { for(j=0; j < 10; j++) System.out.print(riders[i][j] + " "); System.out.println(); } System.out.println(); System.out.println("Riders per trip on the weekend:"); for(i=5; i < 7; i++) { for(j=0; j < 2; j++) System.out.print(riders[i][j] + " "); System.out.println(); }
Assign Array Reference when you assign one array reference variable to another you are not causing a copy of the array to be made, nor are you causing the contents of one array to be copied to the other. int nums1[] = new int[10]; int nums2[] = new int[10]; for(i=0; i < 10; i++) nums1[i] = i; for(i=0; i < 10; i++) nums2[i] = -i; nums2 = nums1; // now nums2 refers to nums1