How to use Array in java?

Array is a collection of similar type of elements. However big an array, its element are always stored in contiguous memory location. This is very important point which we would discuss in more detail later on.

To begin with, like other variable, an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. In this we have done with the statement.

An array is also known as a sub-scripted variable.

Before using an array, its type and dimension must be declared.

It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.

 

Types of Array in java

There are two types of array.

  • Single Dimensional Array
  • Multidimensional Array

 

  •  Single Dimensional Array in java

Step 1: Lets open a notepad and write the following code.

//Code

class demo

{

public static void main(String arg[])

{

char arr[]={30,20,40,50,10};

for(int x:arr)

{

System.out.println(x);

}

}

}

Step 2: Name it as “arry.java” and save the file on any location. I saved at “C:\kiran\program”.

Step 3: Open command prompt (Press window + R and write cmd and hit OK).

Step 4: Set the path.

Step 5: Now write the following code for checking my java file is compiling or not.

javac arry.java.

1

My java program compiled successfully.

 

Step 6: Write the following code in command prompt. Press enter and see the output.

java demo

// demo is a class name which is written in my arry.java file.

Output:

2

 

Step 7: Now code for array for each.

Variable length argement(….a)

//code

class demo

{

void show(int…a)

{

for(int z:a)

{

System.out.println(z);

}

}

public static void main(String arg[])

{

demo d=new demo();

d.show(10,20,30,40,50,60,70,80,90,100);

}

}

 

Step 8: Now write the following code for checking my java file is compiling or not.

javac arr.java.

01

My java program compiled successfully.

 

Step 9: Write the following code in command prompt. Press enter and see the output.

java demo

// demo is a class name which is written in my arr.java file.

Output:

02

 

 

  • Multidimensional Array in java

Step 10: Lets open a notepad and write the following code.

//Code

class test

{

public static void main(String[] args)

{

int[][] values = new int[4][3];

values[1][0] = 1;

values[2][1] = 2;

values[3][2] = 3;

for (int i = 0; i < values.length; i++)

{

int[] total = values[i];

for (int j = 0; j < total.length; j++)

{

System.out.print(total[j] + ” “);

}

System.out.println();

}

}

}

Step 11: Name it as “marray1.java” and save the file on any location. I saved at “C:\kiran\program”.

Step 12: Now write the following code for checking my java file is compiling or not.

javac marray1.java.

mar

My java program compiled successfully.

 

Step 13: Write the following code in command prompt. Press enter and see the output.

java test

// demo is a class name which is written in my marray1.java file.

Output:

marr

Happy coding..

Tags: , ,