ListIterator : add()

The add() method is used to add objects to a collection. This method always takes an object as an argument. If we wish to store primitive data type values, we have to convert them into objects and then add them to the collection. Program       Program Source import java.util.ArrayList; import java.util.ListIterator; public class … Read more…

ListIterator : hasNext(), next(), hasPrevious() and previous()

ListIterator is used just like Iterator. For collections that implement List, you can also obtain an iterator by calling listIterator(). A list iterator gives you the ability to access the collection in either the forword or backword direction and lets you modify an element. The hasPrevious( ) method returns true if there is a previous … Read more…

Iterator : hasNext() and next()

Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time. In general, to use an iterator to cycle through the contents of a collection, follow these steps: … Read more…

Java ArrayList toArray

The toArray() method that both array collections implement does this for you. You typically use the method like this: String[] getarray = new String[array1.size()]; getarray = array1.toArray(getarray); The toArray() method here returns an array of type String[] containing all the elements from names in the correct sequence. Program       Program Source import java.util.ArrayList; … Read more…

Java ArrayList

The java collections framework is made available by the java.util package. Many useful interface, such as Collection, List and Set, are defined in this framework. Many useful classes, such as Collections, LinkedList, HashSet and Treeset are defined in the framework. THE ARRAYLIST CLASS The ArrayList class extends the AbstractList class, which is a predefined class. … Read more…