Related Topics

Data Structure
dataType[][] arrayName = new dataType[rows][columns];
For example, to declare and initialize a two-dimensional array of integers with 3 rows and 4 columns:
int[][] matrix = new int[3][4];
This creates a 3×4 matrix with 12 integer elements in total. Each row and column is indexed starting from 0. You can access individual elements of the array using their indices, as in matrix[0][0]
for the element in the first row and first column.
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// Loop through rows
for (int i = 0; i < arr.length; i++) {
// Loop through columns
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println(); // Move to the next line after printing a row
}
This code will print the following output:
1 2 3
4 5 6
7 8 9
In this example, we have a two-dimensional array arr
with three rows and three columns. The outer loop iterates over the rows, and the inner loop iterates over the columns in each row. The System.out.print()
statement is used to print each element in the array, separated by a space. After printing all the elements in a row, we move to the next line using System.out.println()
to print the next row on a new line.
public class ArraySumExample {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
System.out.println("Sum of elements in the array: " + sum);
}
}
In this example, we initialize an array of integers and a variable sum
to 0. Then, we use a for loop to iterate through the array and add each element to the sum
variable. Finally, we print out the value of the sum
variable to get the sum of all the elements in the array.
int[] arr = {5, 3, 9, 2, 8};
int max = arr[0]; // assume first element is maximum
int min = arr[0]; // assume first element is minimum
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("Maximum element: " + max);
System.out.println("Minimum element: " + min);
In this example, we initialize the maximum and minimum values to the first element of the array. Then, we loop through the rest of the elements and update the maximum and minimum values if we find a larger or smaller element, respectively. Finally, we print the maximum and minimum values. Note that this code assumes that the array is not empty. If the array can be empty, you should add appropriate checks to handle that case.
ArrayList<Integer> numbers = new ArrayList<Integer>();
Adding and removing elements: Adding and removing elements in an array requires shifting the entire array, which can be an expensive operation. With an ArrayList, you can add or remove elements at any position without having to shift the entire list. You can add an element to an ArrayList using the
add
method, and remove an element using theremove
method.Memory allocation: Arrays are allocated in contiguous memory locations, which can be inefficient if you need to resize the array frequently. ArrayLists use a dynamically allocated array behind the scenes, which allows for more efficient memory management.
Random access: Accessing an element in an array is a constant-time operation, while accessing an element in an ArrayList is a linear-time operation. This means that if you need to access elements randomly, an array might be a better choice. However, if you need to add or remove elements frequently, an ArrayList might be a better choice.
Overall, the choice between an array and an ArrayList depends on the specific requirements of your program. If you need a fixed-size data structure and random access to elements, an array might be a better choice. If you need a dynamic data structure that can grow or shrink as needed, an ArrayList might be a better choice.
import java.util.ArrayList;
// Declare and initialize an empty ArrayList of Strings
ArrayList<String> myArrayList = new ArrayList<String>();
// Declare and initialize an ArrayList with initial values
ArrayList<String> myArrayList = new ArrayList<String>(Arrays.asList("one", "two", "three"));
In the first example, an empty ArrayList of Strings is created using the default constructor. In the second example, an ArrayList is created with initial values using the Arrays.asList()
method.
// Declaring and initializing an ArrayList
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("apple"); // Adding an element
fruits.add("banana");
fruits.add("orange");
System.out.println(fruits); // Output: [apple, banana, orange]
fruits.add(1, "grape"); // Adding an element at index 1
System.out.println(fruits); // Output: [apple, grape, banana, orange]
fruits.remove(2); // Removing the element at index 2
System.out.println(fruits); // Output: [apple, grape, orange]
fruits.remove("grape"); // Removing the element "grape"
System.out.println(fruits); // Output: [apple, orange]
In the examples above, we first declare and initialize an ArrayList of type String called fruits
. We then add elements to the list using the add()
method. To add an element at a specific index, we can use the add(index, element)
method. We can remove elements using the remove()
method by either specifying the index or the element to be removed.
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("apple");
list.add("banana");
list.add("orange");
System.out.println(list.contains("banana")); // prints true
System.out.println(list.contains("grape")); // prints false
}
}
In this example, we create an ArrayList called list
and add three String elements to it. We then use the contains
method to check whether the ArrayList contains the elements “banana” and “grape”. The output shows that “banana” is in the ArrayList, while “grape” is not.
import java.util.ArrayList;
import java.util.Collections;
public class Example {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(10);
numbers.add(5);
numbers.add(7);
numbers.add(3);
numbers.add(8);
System.out.println("Before sorting: " + numbers);
Collections.sort(numbers);
System.out.println("After sorting: " + numbers);
}
}
Output:
Before sorting: [10, 5, 7, 3, 8]
After sorting: [3, 5, 7, 8, 10]




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.