Arrays in Java
Arrays
Arrays are the collection of similar types of data. They are used to store multiple values in a single variable. To declare an array, you can define variable types with a square bracket.
Syntax
// Array Syntax
datatype[] arrayname;
Example
public class Main {
public static void main(String[] args) {
// Array Example
String [] names = {"Anar Singh Basnet", "Rama Aryal", "Rachana", "Ram Adhikari", "Munu"};
int [] grades = {50,60,70,80,90};
System.out.println(names[1]);
System.out.println(grades[0]);
}
}
How To Access The Element Of Array
You can use an array index to access the element of an array.
Note: An Array index always starts with 0. That means the first element of an array is at index 0.
Example
public class Main {
public static void main(String[] args) {
// create an array
String [] names = {"Anar", "Rama", "Rachana", "Ram", "Munu"};
// accessing each array elements
System.out.println("Name at index 0 "+ names[0]);
System.out.println("Name at index 1 "+ names[1]);
System.out.println("Name at index 2 "+ names[2]);
System.out.println("Name at index 3 "+ names[3]);
System.out.println("Name at index 4 "+ names[4]);
}
}
Change Array Element
You can change the array element’s value by using the array index.
Example
public class Main {
public static void main(String[] args) {
// Array Example
String [] names = {"Anar", "Rama", "Rachana", "Ram", "Munu"};
names[0] = "Hari Sharma"; // It will change Anar Singh Basnet to Hari Sharma
System.out.println("Name at index 0 "+ names[0]);
}
}
Find Length Of Array
You can find the array’s length using the length method.
Example
public class Main {
public static void main(String[] args) {
// Array Example
String [] names = {"Anar", "Rama", "Rachana", "Ram", "Munu"};
names[0] = "Hari Sharma"; // It will change Anar Singh Basnet to Hari Sharma
System.out.println(names.length); // output will be 5
}
}
Loop Through Array
You can also loop through each element of the array. You can use for loop and length property to specify how many times the loop will run.
public class Main {
public static void main(String[] args) {
// create an array
String [] names = {"Anar", "Rama", "Rachana", "Ram", "Munu"};
// looping each array elements
for(int i = 0; i< names.length; i++){
System.out.println("Name at index "+ i +" "+ names[i]);
}
}
}
Using Foreach
There is also foreach loop that is used exclusively to loop through elements in arrays.
Example
public class Main {
public static void main(String[] args) {
// Array Example
String [] names = {"Anar", "Rama", "Rachana", "Ram", "Munu"};
for(String name: names){
System.out.println("Name "+ name);
}
}
}
Example: Find sum and average of array element
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 5, 100, 9};
int total = 0;
Double avg;
// access all elements using for each loop
// add each element in total
for (int number: numbers) {
total += number;
}
// get the total number of elements
int arrayLength = numbers.length;
// calculate the avg
// convert the avg from int to double
avg = ((double)total / (double)arrayLength);
System.out.println("Total is = " + total);
System.out.println("Average is = " + avg);
}
}