An index is the position of an item in an array. The first item is at position 0, the second at position 1, and so on.
int[] marks = {50, 55, 60, 70, 80}; System.out.println(marks[0]); // Output: 50 System.out.println(marks[4]); // Output: 80
An array is a collection of items of the same type. You start counting from 0 up to one less than the total number of items.
// Create an array with 5 elements int[] marks = {10, 20, 30, 40, 50};
In Java, arrays store a list of items of the same type. You can create arrays in different ways.
int[] age = {20, 21, 30}; int[] marks = new int[3]; marks[0] = 50; marks[1] = 70; marks[2] = 93;
Arrays have a fixed size, but you can change the value of an item at any index.
int[] nums = {1, 2, 0, 4}; // Change the value at index 2 nums[2] = 3;
An ArrayList is a flexible array that can grow in size. You can add, remove, and change items easily.
// Import the ArrayList class import java.util.ArrayList; // Create an ArrayList called students ArrayList<String> students = new ArrayList<String>();
To modify an ArrayList, you can add, remove, or change items by specifying their position or value.
class Animal { // The sound method is overridden by subclasses public void sound() { System.out.println("Animal makes a sound"); } } class Cat extends Animal { @Override public void sound() { System.out.println("Meow"); } } class Dog extends Animal { @Override public void sound() { System.out.println("Woof"); } } class Pig extends Animal { @Override public void sound() { System.out.println("Oink"); } } public class Main { public static void main(String[] args) { Animal cat = new Cat(); Animal dog = new Dog(); Animal pig = new Pig(); Animal[] animals = {cat, dog, pig}; for (Animal animal : animals) { animal.sound(); } } }
The for-each loop is used to go through each item in an array or ArrayList easily, without needing to know the size or index.
// Array of numbers int[] numbers = {1, 2, 3, 4, 5}; // Loop through each number for (int num : numbers) { System.out.println(num); }
Nested loops are loops inside other loops. The inner loop completes all its iterations before the outer loop moves to the next iteration.
for (int outer = 0; outer < 3; outer++) { System.out.println("Outer index: " + outer); for (int inner = 0; inner < 4; inner++) { System.out.println(" Inner index: " + inner); } }
2D arrays are arrays of arrays. They are declared with two sets of square brackets.
int[][] twoDIntArray; String[][] twoDStringArray; double[][] twoDDoubleArray;
To access an item in a 2D array, use two indices: the first for the row and the second for the column.
int[][] arr = {{1, 2, 3}, {4, 5, 6}}; int retrieved = arr[1][0]; // Output: 4
Initializer lists let you set the values of a 2D array when you create it. You can define values in curly brackets.
int[][] matrix = {{1, 2}, {3, 4}};
To change an element in a 2D array, you assign a new value to a specific row and column.
int[][] matrix = {{1, 2}, {3, 4}}; matrix[0][1] = 10; // Change value at first row, second column to 10
Row-major order means going through each row one at a time. Use nested loops to traverse a 2D array row by row.
for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.println(matrix[i][j]); } }
Column-major order means going through each column one at a time. Use nested loops to traverse a 2D array column by column.
for (int i = 0; i < matrix[0].length; i++) { for (int j = 0; j < matrix.length; j++) { System.out.println(matrix[j][i]); } }
You can use enhanced for loops to easily traverse a 2D array. The outer loop goes through rows, and the inner loop goes through columns.
for (String[] rowOfStrings : twoDStringArray) { for (String s : rowOfStrings) { System.out.println(s); } }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!