In this article we learn how to declare instantiate and initialize the array. Arrays are nicely organized lists of data. Think of a numbered list that starts at zero and extends one line every time you add something to the list. Arrays are useful for any number of situations because they’re treated as a single hunk of data.

Here we will see how to declare array and how to initialize them. If you want to know what is array, types of arrays and working of arrays then click here.

We can declaring, instantiating and initializing the java array together by:

int a[]= {33,3,4,5};//declaration, instantiation and initialization

Let’s see the simple example to print this array.

class Testarray1
{
public static void main(String args[])
{
int a[]=
{33,3,4,5};//declaration, instantiation and //printing array
for(int i=0;i<a.length;i++)//length is the property System.out.println(a[i]);
}                                                                                                                                                     }


Output:

33
3
4
5

declare, instantiate and initialize the Arrays

Declare, Instantiate and Initialize the Array & Passing Array to method in java:

We can pass the java array to method so that we can reuse the same logic on any array. Here we will see the how to pass the array as a parameter to the method.

Let’s see the simple example to get minimum number of an array using method.

Here we pass the integer array a[] to the static method min() and find the minimum integer number in that array a[] and after finding the minimum number print this minimum number.

class Testarray2
{
static void min(int arr[])
{
int min=arr[0];
for(int i=0;i<4; i++)
{
 if(min>arr[i])
 {
 min=arr[i];
 }
}
 System.out.println(min);

}
public static void main(String args[])
{
int a[]={33,3,4,5};
min(a);//passing array to method
}
}


Output:

3
Passing Array to method in java

If you are searching for different operations on array like searching, sorting then stop the searching and click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top