I want to inquire what is the major difference between the usage of ArrayList and Array in the Java programming language. I would appreciate good responses and would be enthralled to continue the discussion further upon interest.
Regards,
Ali
I want to inquire what is the major difference between the usage of ArrayList and Array in the Java programming language. I would appreciate good responses and would be enthralled to continue the discussion further upon interest.
Regards,
Ali
A major difference is, an Array has a fixed size, whereas an ArrayList can grow dynamically.
Arrays can hold primitive types like int, whereas ArrayLists cannot they can only hold objects
Also, Arrays are not part of a class, therefore they have no methods that you can use to change them, you need to refer to each element in the array and do whatever you want with each. i.e. there is no add/remove for an array.
ArrayList is a class that is an implementation of the List interface. It conains many methods that allow you to add/remove/compare its elements much easier than with an Array. To add an element, you can simply use the add() method, e.g. list.add(Object),
The remove method can be used similarily. For more info on the methods available, and other info about Collections in general, check out this
http://download.oracle.com/javase/tutorial/collections/interfaces/collection.html
For using the array, u should know the number of elements. As while creating instance you to mention the size of array.
Array list is dynamic. Just initialize arraylist and then add object or primitive data type using add method.
String[] empNames = new String[2];
empNames[0] = "joe";
empNames[1] = "ram";
for (int i = 0; i < empNames.length; i++) {
System.out.println("Name:"+empNames[i]);
}
ArrayList<String> list = new ArrayList<String>();
list.add("joe");
list.add("ram");
for (int i = 0; i < list.size(); i++) {
System.out.println("Name:"+list.get(i));
}
I want to inquire what is the major difference between the usage of ArrayList and Array in the Java programming language. I would appreciate good responses and would be enthralled to continue the discussion further upon interest.
Regards,
Ali
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.