CSC262-Discussion 3 Arrays and ArrayLists in Java
In Java, arrays and ArrayLists are both used to store multiple values, but they work differently. An array is a fixed-size structure that stores elements of the same type. Once an array is created, its size cannot be changed. For example, if an array is created to hold three names, it can only hold three names unless a new array is created. Arrays use index positions to access values, and the length of an array is found with the .length property. Oracle explains that an array holds a fixed number of values of a single type, and its length is established when the array is created (Oracle, n.d.-a).
An ArrayList is different because it can grow or shrink while the program is running. It is part of the java.util package and provides methods such as add(), remove(), get(), and size(). This makes ArrayLists more flexible when the number of elements may change. According to Oracle’s documentation, ArrayList is a resizable-array implementation of the List interface (Oracle, n.d.-b).
The main conceptual difference is that arrays are simpler and usually better when the number of elements is known ahead of time. ArrayLists are better when the number of elements may change. Arrays can store primitive data types directly, such as int or double, while ArrayLists store objects, meaning primitive values must use wrapper classes such as Integer or Double. Overall, arrays are useful for fixed data, while ArrayLists are better for dynamic collections.
This project demonstrates these differences by creating an array of names and an ArrayList of names. The array shows fixed size behavior, while the ArrayList demonstrates adding, removing, and checking the size dynamically.
References
Oracle. (n.d.-a). Arrays. The Java Tutorials. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Oracle. (n.d.-b). ArrayList. Java Platform SE API Specification. https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/ArrayList.html