Arrays in Java

Arrays in Java, What is Array in Java, Declare Arrays, Java Array built-in methods, advantages of Java Arrays, and Drawbacks of Java Arrays.

Java Arrays

• In Java, Array is an Object that holds a fixed number of values of a single data type

• The length of the Array is established when the Array is created.

• Array length is fixed and index starts from zero to n-1,

Declaration of Arrays

1st Method:

Syntax:

dataType arrayName[]; Declare an Array/ Create an Array
arrayName = new dataType[size]; //Define size
arrayName[index] = value; //Assign a value
.
.
.

Example 1:

int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;
a[2] = 30;

System.out.println(a[1] + a[2]);//50

Example 2:

Assign values to Array elements that more than the length of Array (Run-time Error).

int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[10] =40;

System.out.println(a[1] + a[2]);//50

Example 3:

Assign values to some Array Elements only

int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;

System.out.println(a[0] + a[1]);//30

Example 4:

Assign different type of data to Array Elements (Syntax Error)

int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;
//a[2] =10.23;

System.out.println(a[0] + a[1]);//30

2nd Method

Syntax:

dataType [] arrayName = new dataType [size]; //Create an Array with length
arrayName[index] = value; //Assign value
.
.

Example:

int [] a = new int [3];
a[0] =10;
a[1] =20;
a[2] =30;
System.out.println(a[1] + a[2]);//50

3rd Method

Syntax:

dataType [] arrayName = {value1, value2, value3, value4};//Create an Array with Initialization

Example:

int [] a= {10, 20, 30, 40};
System.out.println(a[1] +a[3]);//60

Creating different type Arrays

int [] a= {10, 20, 30, 40, 50}; //Array of Integers

char [] b= {‘A’, ‘S’, ‘1’, ‘*’};//Array of Characters

String [] c = {“UFT”, “Selenium”, “RFT”, “SilkTest”};//Array of Strings

double [] d ={1.234, 3.456, 6.45, 7.890}; // Array of decimal point values

boolean [] e = {true, true, false, true, false}; //Array of Boolean Values / Logical Values

System.out.println(a[2]);//30
System.out.println(d[3]);//7.890
System.out.println(e[0]);//true

Two-Dimensional Array

data_type [] [] array_name = new data_type[row_size][column_size];

int[][] myarray = new int[2][2];
myarray[0][0] = 1;
myarray[0][1] = myarray[1][0] = 0;
myarray[1][1] = 1;

Operations on Arrays

1. Find an Array length.

Example:

int [] a= {10, 20, 30, 40, 50};
System.out.println(a.length);//5

2. Copy an Array to a String

int [] a= {10, 20, 30, 40, 50};
String str= Arrays.toString(a);
System.out.println(str);

String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”};
String str = Arrays.toString(a);
System.out.println(str);

3. Print an Array using for loop

public static void main(String[] args) {
String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”, “LoadRunner”};

for (int i=0; i<=a.length-1; i++){
System.out.println(a[i]);

4. Print an Array using enhanced for loop

String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”, “LoadRunner”};

for (String val: a){
System.out.println(val);
}

5. Check if an Array contains a certain value or not?

String [] a = {“UFT”, “Selenium”, “RFT”, “SilkTest”, “LoadRunner”};

boolean val = Arrays.asList(a).contains(“UFT”);
boolean val2 = Arrays.asList(a).contains(“Java”);
System.out.println(val);//true
System.out.println(val2);//false

 


Java Language Syllabus

Java Videos

Follow me on social media:

2 thoughts on “Arrays in Java”

Comments are closed.