000webhost logo
Core Java test2 Java Tech Tutorials



Arrays and it's Implementation

Core Java

Arrays in Java

Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.

When processing array elements, we often use either for loop or foreach loop because all of the elements in an array are of the same type and the size of the array is known.

//How to Create Array
double[] myList;         // preferred way.
or
double myList[];         //  works but not preferred way.

//Create Array using new operator
dataType[] arrayRefVar = new dataType[arraySize];


//you can also create araya like this
dataType[] arrayRefVar = {value0, value1, ..., valuen};


java_array.jpg

Example of Array in Java - create, initialize and process an arrays

public class TestArray {

   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};

      // Print all the array elements
      for (int i = 0; i < myList.length; i++) {
         System.out.println(myList[i] + " ");
      }
      // Summing all elements
      double total = 0;
      for (int i = 0; i < myList.length; i++) {
         total += myList[i];
      }
      System.out.println("Total is " + total);
      // Finding the largest element
      double max = myList[0];
      for (int i = 1; i < myList.length; i++) {
         if (myList[i] > max) max = myList[i];
      }
      System.out.println("Max is " + max);
   }
}

	Output :-
	1.9
	2.9
	3.4
	3.5
	Total is 11.7
	Max is 3.5

Multi-Dimensional Array :-



To declare a multidimensional array variable, specify each additional index using another set of square brackets.
Ex: int twoD[ ][ ] = new int[4][5] ;
When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension. You can allocate the remaining dimensions separately. In Java the length of each array in a multidimensional array is under your control.

		int twoD[][] = new int[4][];

		twoD[0] = new int[5];

		twoD[1] = new int[6];

		twoD[2] = new int[7];

		twoD[3] = new int[8];

public class Array2DTest {
   public static void main(String[] args) {
      int[][] grid = new int[12][8];   // A 12x8 grid, in [row][col] or [y][x]
      int numRows = grid.length;       // 12
      int numCols = grid[0].length;    // 8

      // Fill in grid
      for (int row = 0; row < numRows; ++row) {
         for (int col = 0; col < numCols; ++col) {
            grid[row][col] = row*numCols + col + 1;
         }
      }

      // Print grid
      for (int row = 0; row < numRows; ++row) {
         for (int col = 0; col < numCols; ++col) {
            System.out.printf("%3d", grid[row][col]);
         }
         System.out.println();
      }
   }
}

For method overloading and Overriding please referer OOP concept




Click here to access OOp's Concept

feel free to ask any of your doubts