Array is a collection of similar data which is stored in continuous memory addresses. They are used to store multiple values in a single variable and its values can be fetched using index.
You can declare an array by adding square brackets to the datatype.
Syntax
data-type[] array-name; // one dimensional array
data-type[][] array-name; // two dimensional array
Example
String[] mobiles = {"iPhone", "Samsung", "OnePlus"};
for (int i = 0; i < mobiles.length; i++) {
System.out.println(mobiles[i]);
}
// below is also valid and can be used instead of traditional for loop
for (string i : mobiles) {
System.out.println(i);
}
How to access Array elements
Array elements can be accessed using index. Index starts from 0 to size-1.
public class MyClass {
public static void main(String[] args) {
String[] mobiles = {"iPhone", "Samsung", "OnePlus", "Nokia"};
System.out.println(mobiles[1]);
}
}
Check result here
How to change a particular element in an array
To change a specific element in an array, refer it's index.
public class MyClass {
public static void main(String[] args) {
String[] mobiles = {"iPhone", "Samsung", "OnePlus", "Nokia"};
mobiles[2] = "Oppo";
for (int i = 0; i < mobiles.length; i++) {
System.out.println(mobiles[i]);
}
}
}
Check Result here
what will happen if you refer an index which is greater than array length?
public class MyClass {
public static void main(String[] args) {
String[] mobiles = {"iPhone", "Samsung", "OnePlus", "Nokia"};
mobiles[4]= "Vivo";
for (int i = 0; i < mobiles.length; i++) {
System.out.println(mobiles[i]);
}
}
}