Arrays In Java - Details of arrays In Java - Selenium with java Tutorials

Java has become one of the most widely used programming languages in the world. It is the most widely used language by selenium experts for selenium testings as it is highly user-friendly and makes testing easier. Java has many special features that make it user-friendly and arrays in Java are one such special feature.

The term “Array” denotes a group of similar types of elements that have almost the same memory location. In java, an array is an object that contains elements of similar data type. More clearly, an array in Java can be defined as a data structure where similar objects are stored. An Array in java is index based and the first element will be stored in 0 indexes.

The advantage of an array in java is that it makes retrieval or sorting of code easier and it allows random access. At the same time, it has one disadvantage too. Only fixed size elements can be stored in an array and it will not grow as the program runs.

Different Types of Arrays in Java

Basically, there are two types of arrays in java and they are one and two-dimensional arrays. They are better known as a single dimensional array and multi-dimensional array.

An Array can be passed to methods in java and so that the logic can be reused on any array.

Example for Single Dimensional Array

class Testarray{

public static void main(String args[]){

int a[]=new int[5];//declaration and instantiation

a[0]=10;//initialization

a[1]=20;

a[2]=70;

a[3]=40;

a[4]=50;

//printing array

for(int i=0;i<a.length;i++)//length is the property of array

System.out.println(a[i]);

}}

Example for Multi-dimensional Array

class Testarray3{

public static void main(String args[]){

//declaring and initializing 2D array

int arr[][]={{1,2,3},{2,4,5},{4,4,5}};

//printing 2D array

for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

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

}

System.out.println();

}

}}

Leave a Reply

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