1. Size: Arrays have a fixed size, determined at the time of their creation, which cannot be changed later. In contrast, ArrayLists are dynamic in size and can grow or shrink dynamically as elements are added or removed.
2. Type: Arrays can store elements of any type, including primitive types and objects. ArrayLists, on the other hand, can only store objects (reference types) and not primitive types. However, through autoboxing, primitive types can be automatically converted to their corresponding wrapper classes and stored in ArrayLists.
3. Flexibility: Arrays provide more flexibility in terms of direct access to individual elements. Elements in an array can be accessed and modified using indexes. ArrayLists, on the other hand, provide additional methods and functionality for adding, removing, and searching elements. They also automatically handle resizing and reordering of elements.
4. Automatic Resizing: ArrayLists automatically resize themselves when the number of elements exceeds their capacity. When an ArrayList is resized, a new underlying array is created with a larger capacity, and elements are copied to the new array. Arrays, on the other hand, do not resize automatically, and if more elements need to be accommodated, a new array must be created and elements from the old array must be copied manually.
5. Methods and Functionality: ArrayLists provide a wide range of methods for manipulating and accessing elements, such as add(), remove(), get(), size(), and more. Arrays have limited built-in methods and require manual implementation of functionalities like resizing, adding, or removing elements.
Example:
Here's an example that demonstrates the usage and differences between ArrayList and Array in Java:
javaimport java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Array
int[] arr = new int[3]; // Create an array with a fixed size of 3
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
System.out.println("Array elements: " + Arrays.toString(arr));
// ArrayList
ArrayList<Integer> arrayList = new ArrayList<>(); // Create an ArrayList
arrayList.add(10);
arrayList.add(20);
arrayList.add(30);
System.out.println("ArrayList elements: " + arrayList);
// Adding elements
arrayList.add(40); // Adding an element to ArrayList
int[] newArr = Arrays.copyOf(arr, 4); // Creating a new array and copying elements
newArr[3] = 40; // Adding an element to the array
System.out.println("Updated ArrayList elements: " + arrayList);
System.out.println("Updated array elements: " + Arrays.toString(newArr));
// Removing elements
arrayList.remove(1); // Removing an element from ArrayList
int[] newArr2 = new int[3];
System.arraycopy(arr, 0, newArr2, 0, 1); // Copying elements to a new array
System.arraycopy(arr, 2, newArr2, 1, 1); // Omitting element at index 1
System.out.println("Updated ArrayList elements after removal: " + arrayList);
System.out.println("Updated array elements after removal: " + Arrays.toString(newArr2));
}
}
The output of the program will be:
outputArray elements: [10, 20, 30]
ArrayList elements: [10, 20, 30]
Updated ArrayList elements: [10, 20, 30, 40]
Updated array elements: [10, 20, 30, 40]
Updated ArrayList elements after removal: [10, 30, 40]
Updated array elements after removal: [10, 30, 40]
Explanation: The program first creates an array arr with a fixed size of 3 and initializes its elements. It then creates an ArrayList arrayList and adds elements to it using the add() method.
After that, elements are added to both the ArrayList and the array using different approaches. The updated elements are displayed using the toString() method for the ArrayList and the Arrays.toString() method for the array.
Next, an element is removed from both the ArrayList and the array. The updated elements are displayed again, showing the changes made after the removal.
The final output demonstrates the original and updated elements of both the ArrayList and the array, confirming the modifications made during the program execution.
By understanding the differences between ArrayList and Array, you can choose the appropriate data structure based on your specific needs, such as flexibility, dynamic resizing, or direct element access.
No comments:
Post a Comment