Thursday, 17 August 2017

Java Araay

An array is a container that holds values of one single data type. For example, you can create an array that can hold 50 values of int type.

Array is a fundamental construct in Java that permits you to store and retrieve large number of values easily.

How to declare an array?

Here's how you can declare an array in Java:

Syntax  for declaring array in java.

Syntax
Data type[]   arrayName;
arrayName = New  datatype[10]

Example of declaring array in java:
Example
Int[]   arr;
Arr = new int[5]

Another way to declaring array in java:
Syntax
int[] age = new int[5];


Java Array initialize

How to initialize array in java:
Syntax
int[] age = {10, 25, 24, 36, 45,41};


Array index


Example Code
class ArrExample
{
   public static void main(String[] args)
   {
      int[] age = {10, 25, 24, 36, 45};

      for (int i = 0; i < 5; i++)
                  {
         System.out.println("Value at index " + i +": " + age[i]);
      }
   }
}
Output:
Value at index 0: 10
Value at index 1: 25
Value at index 2: 24
Value at index 3: 36
Value at index 4: 45


Multidimensional Arrays

Multidimensional arrays, are arrays of arrays.

When you allocate memory for a multidimensional array, you require only specify the memory for the first (leftmost) dimension.

You can allocate the left over dimensions one by one.

In Java the length of each array in a multidimensional array is below your control.

Syntax for how to declare array in java:
Syntax
dataType[][] arrayName = new dataType[size][size];


Example of declaring array in java
Syntax
int[][] intArrays = new int[4][5];


Simple example of two dementional array in java:
Code
class multiarray
  public static void main(String args[])
  { 
     //declaring and initializing  array 
     int arr[][]={{1,2,3},{2,7,5},{3,4,5}}; 
 
    //printing array 
    for(int i=0;i<3;i++)
    { 
                for(int j=0;j<3;j++)
                { 
                                System.out.print(arr[i][j]+" "); 
                } 
                System.out.println(); 
    } 
  }
Output:
1    2   3
2   7    5
3   4   5


In this Tutorial we learned how to create array, how to initialize, how to create two dimensional array, how to create multidimensional array.

1 comments:

  1. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
    big data training in chennai
    hadoop training in chennai

    ReplyDelete