The ArrayList class:
Objects of the generic class ArrayList have some similarity to arrays - they can store a number of references and the data can be accessed via an index. An ArrayList can automatically increase in capacity as necessary, unlike an array, which is of a fixed capacity once it has been declared. The ArrayList object requests more space from the Java run-time system, if necessary.We need to define two important terms in connection with ArrayList objects. The capacity of an ArrayList object is the maximum number of items it can currently hold (without being extended). The size of an ArrayList object is the current number of items stored in the ArrayList, which clearly cannot exceed the current capacity.
The ArrayList class has a number of constructors. For example:
final int INIT_CAPACITY = 100;
//zero-argument constructor
ArrayList<String> list1 = new ArrayList<String>();
//one-argument constructor
ArrayList<Date> list2 = new ArrayList<Date>(INIT_CAPACITY);
The ArrayList constructor with no arguments sets up an empty ArrayList, which is then referenced by list1. The initial capacity is set to a default value. The ArrayList capacity is expanded automatically as needed when items are added.
The second constructor with one argument can be used to specify the initial capacity - in this case 100 elements. This can be more efficient in avoiding repeated work by the system in extending the list, if you know approximately the required initial capacity. Normally, it is best to allow the system to manage the capacity of the ArrayList as required. Any attempt to access the unused part of an ArrayList object will generate an exception.
The first declaration of an ArrayList specifies that it will hold String references while the second specifies that Date references will be stored.
While an ArrayList represents an advance over the use of an array, it is not ideal in its use of memory. As soon as an ArrayList requires more space than its current capacity, the system will allocate more memory space for it. The details of this depend on the implementation of the Java library but, in general, the extension is more than required for the items about to be added. So there will often be empty locations within an ArrayList - that is, the size will often be less than the capacity. This means that it can be a slightly inefficient solution to the problem of storing variable amounts of data.
There are a large number of methods associated with ArrayList objects. Table shows the basic methods for operations such as adding, removing, retrieving and updating items. You can look up the full details in the API documentation.
There are two overloaded add methods and two overloaded remove methods - recall that overloaded methods have the same name but different numbers or types of arguments. One of the add/remove pairs allows adding and removing at a particular index, similar to an array. The other pair behaves more like a list, adding an item to the end of the ArrayList and removing an object from anywhere in the list if it matches the specified object.
![351_array list class.png](https://www.expertsmind.com/../CMSImages/351_array list class.png)
Basic methods of the ArrayList<E> class
![730_array list class1.png](https://www.expertsmind.com/CMSImages/730_array%20list%20class1.png)
Standard utility methods of the ArrayList class
There are some general points to note. The add methods 'make room' within the ArrayList object by moving items along and renumbering the indexes, if necessary. The remove methods 'close up any gap' created by the deletion of an item and renumber the indexes, if necessary. The set method simply overwrites the existing reference, so there is no need to change any indexes and the total size of the ArrayList object is unchanged.
An example of the use of the add method is shown below:
ArrayList<User> list = new ArrayList<User>(); User u = new User("John");
list.add(u);Removing items is straightforward. For example, the code:
User u2 = list.remove(0);
will remove the user object stored at position 0 in the list.
When using methods that search the ArrayList for a matching object, like the remove method listed in Table or the contains method listed in Table, you must ensure that the class for the stored data has a suitable equals method - see the next section for details.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The ArrayList class homework and assignments? Live The ArrayList class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The ArrayList class homework help, java assignment help and The ArrayList class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours