Java Array Operations

Java Array Operations

Array Operations in Java

What is Array?

  • Array is the set of Variables with the same Datatype.
  • Array is the fixed size.
  • Array will be Iterated by the index.

Below is the example for the String. Same like we can create the Array for int and further datatype.

String[] arr= {“Besant”,”Technologies”,”Tambaram”,”Centre”};

Array can’t be extended in size.

Advantages :

  • Code Simplicity
  • Can use the Array for same Datatype.

Disadvantage:

  • Can’t extend the size of the Array.

Example:

String[] str= new String[5]; // Declaration and initiation.
str[0]=”Besant”; //Defining the data
str[1]=”Technologies”;
str[0]=”Tambaram”;
str[2]=”Centre”;
str[3]=”Chennai”;
str[4]=”600045”;
//iterating the data
for(int i=0; i<5;i++)
https://www.gangboard.com/blog/wp-admin/admin.php?page=wpseo_dashboard
System.out.prin(str[i]+”,”);

Output:

Besant, Technologies, Tambaram, Centre, Chennai, 600045

Note:

If we select more than 5 entries, then we will face the ArrayIndexOutodBoundException.

Simple Functions in Array:

Array Length:

To find out the length of the Array.

Example:

String[] alps ={“apple”,”ball”,”cat”,”doll”,”ear”,”flight”};
System.out.print(alps.length);
Ouput: 6

Loop iteration through Array:

To get the values in the array by using the length of the array.

Example:

String[] alps ={“apple”,”ball”,”cat”,”doll”,”ear”,”flight”};
for(int i=0;i<alps.length;i++)
System.out.print(alps[i]+”,”);

Output:

Apple, ball,cat, doll, ear, flight

Multidimensional Array:

Example:

Array like matrix,
int[][]  myMatrix = {{1,2,3,4,5},{6,7,8,9,0}};
int l=myMatrix[1][1];
System.out.println(l)

OutPut:

7

Example with For:

int[][]  myMatrix = {{1,2,3,4,5},{6,7,8,9,0}};
for(int i=0;i<2;i++)//it’s the row
{
for(int j=0;j<4;j++)//it’s the column
{
System.out.print(myMatrix[i][j]+”,”);
}
System.out.println();
}

OutPut:

1,2,3,4,5,

6,7,8,9,0,

Leave a Reply

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

Looking for Online Training