Arrays F Introducing Arrays F Declaring Array Variables F Creating Arrays, and Initializing Arrays F Copying Arrays F Multidimensional Arrays
Introducing Arrays Array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. An Array of 10 Elements of type double
Declaring Array Variables F datatype[] arrayname; Example: int[] myList; F datatype arrayname[]; Example: int myList[];
Creating Arrays arrayName = new datatype[arraySize]; Example: myList = new double[10];
Declaring and Creating in One Step F datatype[] arrayname = new datatype[arraySize]; double[] myList = new double[10]; F datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10];
Initializing Arrays F Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = (double)i; F Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5};
Passing Arrays to Methods F Java uses pass by value to pass parameters to a method. There are important differences between passing a value of variables of primitive data types and passing arrays. F For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method. F For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument.
Copying Arrays?
Copying Arrays Using a loop: int[] sourceArray = {2, 3, 1, 5, 10}; int[] targetArray = new int[sourceArray.length]; for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i];
The arraycopy Utility arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(A1, 0, A2, 0, A1.length );
Multidimensional Arrays F int[][] matrix = new int[10][10]; or int matrix[][] = new int[10][10]; F int[][][] matrix = new int[10][20][30]; for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); }
Ragged Arrays Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} };
Array of arrays int twoD[][] = new int[4][]; twoD[0] = new int[4]; twoD[1] = new int[3]; twoD[2] = new int[2]; twoD[3] = new int[1];
Array of Objects Circle[] circleArray = new Circle[10]; F An array of objects is actually an array of reference variables. F So invoking circleArray[1].findArea() involves two levels of referencing as shown in the next figure. circleArray references to the entire array. circleArray[1] references to a Circle object.
Array of Objects, cont. Invoking circleArray[1].findArea() involves two levels of referencing as shown in the next figure.. circleArray references to the entire array. circleArray[1] references to a Circle object
Summarizing the areas of the circles // Declaration and creation Circle[] circleArray = new Circle[10]; for(int k=0; k < circleArray.length; k++) { circleArray[k] = new Circle(Math.random*100); } // Print radius and calculate the total area double totalArea = 0; for(k=0; k < circleArray.length; k++) { System.out.println(“k=“+k+”) R=“+circleArray[k].getRadius()); totalArea += circleArray[k].findArea(); } System.out.println(“Area=”+ totalArea);