Java Arrays
In Java, an array is used to store a list of elements of the same datatype.
Arrays are fixed in size and their elements are ordered.
Array creation in Java:
In Java, an array can be created in the following ways:
- Using the
{}notation, by adding each element all at once. - Using the
newkeyword, and assigning each position of the array individually.
int[] age = {20, 30, 40};
int[] age= new int[3];
marks[0] = 20;
marks[1] = 30;
marks[2] = 40;
// Keeps the same valuesAccess the Elements of an Array:
You can access an array element by referring to the index number.
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]); // Outputs Volvo
System.out.println(cars[3]); // Outputs MazdaChange an Array Element:
To change an element value, select the element via its index and use the assignment operator to set a new value.
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]); // Now outputs Opel instead of VolvoArray Length:
To find out how many elements an array has, use the length property.
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4Loop Through an Array:
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.print(cars[i]);
} //Output Volvo BMW Ford Mazda
for (String i : cars) {
System.out.print(i);
} //Output Volvo BMW Ford MazdaJava ArrayList:
In Java, an ArrayList is used to represent a dynamic list.
Get Bora Fazli’s stories in your inbox
Join Medium for free to get updates from this writer.
While Java arrays are fixed in size (the size cannot be modified), an ArrayList allows flexibility by being able to both add and remove elements.
// import the ArrayList package
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// create an ArrayList called students
ArrayList<String> students = new ArrayList<String>();
}
}Add Items:
The ArrayList class has many useful methods. For example, to add elements to the ArrayList, use the add() method:
ArrayList<String> students = new ArrayList<String>();
students.add("John");
students.add("Lily");
students.add("Samantha");
System.out.println(cars); //Output [John, Lily, Samantha]Remove an Item:
To remove an element, use the remove() method and refer to the index number.
students.remove(0);To remove all the elements in the ArrayList, use the clear() method.
students.clear();Change an Item:
To modify an element, use the set() method and refer to the index number.
students.set(0, "Tony");ArrayList Size:
To find out how many elements an ArrayList have, use the size method.
students.size();Loop Through an ArrayList:
Loop through the elements of an ArrayList with a for loop, and use the size() method to specify how many times the loop should run.
ArrayList<String> students = new ArrayList<String>();
students.add("John");
students.add("Lily");
students.add("Samantha");
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i)); //Output [John, Lily, Samantha]
for (String i : students) {
System.out.println(i); //Output [John, Lily, Samantha]Multi-Dimensional Arrays:
In Java, 2D arrays are stored as arrays of arrays. Therefore, the way 2D arrays are declared is similar 1D array objects. 2D arrays are declared by defining a data type followed by two sets of square brackets.
In Java, when accessing the element from a 2D array using arr[ ][ ], the first index can be thought of as the desired row, and the second index is used for the desired column.
Just like 1D arrays, 2D arrays are indexed starting at 0.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
System.out.println(myNumbers[1][2]); // Outputs 7Change Element Values:
You can also change the value of an element.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
myNumbers[1][2] = 9;
System.out.println(myNumbers[1][2]); // Outputs 9 instead of 7Loop Through a Multi-Dimensional Array:
We can also use a for loop inside another for loop to get the elements of a two-dimensional array.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
for (int i = 0; i < myNumbers.length; ++i) {
for(int j = 0; j < myNumbers[i].length; ++j) {
System.out.println(myNumbers[i][j]);
}
}





