In Java, a List and an Array are both used to store and manipulate collections of elements. However, there are several differences between them in terms of their characteristics and usage.
Let's explore the differences between a List and an Array in Java, followed by a comparison of ArrayList and Array with examples.
List vs. Array:
1. Flexibility: Arrays have a fixed size defined at the time of initialization and cannot be resized dynamically. On the other hand, Lists, such as ArrayList, can grow or shrink dynamically as elements are added or removed.
2. Type Safety: Arrays can store both primitive data types and objects, while List implementations (e.g., ArrayList) are designed to store objects only. This allows Lists to provide type safety through the use of generics.
3. Functionality: Lists provide additional built-in methods and functionality compared to plain arrays. For example, Lists offer methods for adding, removing, searching, and sorting elements, whereas arrays require manual implementation of such operations.
4. Memory Management: Arrays are a low-level construct in Java and store elements in contiguous memory locations. They have a fixed memory footprint based on their declared size. In contrast, Lists use dynamic memory allocation and manage memory automatically as elements are added or removed.
5. Performance: Arrays generally offer better performance in terms of direct element access, as they provide constant-time random access using an index. Lists, on the other hand, may have a slightly higher overhead due to their dynamic resizing and additional functionality. ArrayList vs. Array
Example:
Here's an example that demonstrates the difference between an ArrayList and an Array in Java:
import java.util.ArrayList;
import java.util.List;
public class ArrayListVsArrayExample {
public static void main(String[] args) {
// ArrayList example
List arrayList = new ArrayList<>();
arrayList.add("apple");
arrayList.add("banana");
arrayList.add("orange");
System.out.println("ArrayList: " + arrayList);
// Accessing element in ArrayList
String fruit1 = arrayList.get(0);
System.out.println("Fruit at index 0: " + fruit1);
// Updating element in ArrayList
arrayList.set(1, "grape");
System.out.println("Updated ArrayList: " + arrayList);
// Array example
String[] array = new String[3];
array[0] = "apple";
array[1] = "banana";
array[2] = "orange";
System.out.println("Array: " + java.util.Arrays.toString(array));
// Accessing element in Array
String fruit2 = array[0];
System.out.println("Fruit at index 0: " + fruit2);
// Updating element in Array
array[1] = "grape";
System.out.println("Updated Array: " + java.util.Arrays.toString(array));
}
}
OUTPUT :
ArrayList: [apple, banana, orange]
Fruit at index 0: apple
Updated ArrayList: [apple, grape, orange]
Array: [apple, banana, orange]
Fruit at index 0: apple
Updated Array: [apple, grape, orange]
In this example, we create an ArrayList called arrayList and an Array called array. We add elements to the ArrayList using the add() method, and to the Array using assignment. We access elements in both collections using indexing (e.g., get() for ArrayList, and direct indexing for Array) and update elements using set() for ArrayList and assignment for Array. Finally, we print the ArrayList and Array to compare the results.
ArrayList provides dynamic resizing and additional methods for manipulation, while Array has a fixed size and requires manual
No comments:
Post a Comment