0

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

Edited by aligajani: n/a

3
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by dheeraj208
1

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

Edited by Akill10: n/a

0

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

Edited by ~s.o.s~: Added code tags, please learn to use them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.