As tostring is a polymorphic method that is declared

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: uot;\n", the toString() method is invoked for the referenced object. As toString() is a polymorphic method that is declared in class Object and redefined in its subclasses, the method is executed so that first the type of the referenced object is checked, and then the toString() method of the class of the referenced object is invoked. On the following pages you can find example programs,, and that demonstrate the use of ArrayList-based arrays and ArrayList methods. Program in the previous chapter also uses class ArrayList. In the following section there is a program named, but because that program introduces a new concept called interface, it is presented in a new section. Briefly described, the following are the most important ArrayList methods: • To add elements to an ArrayList array you can use methods add() and addAll(). When these methods are called, the number of elements in the ArrayList array is increased. The more powerful of these methods is addAll() because with that method you can insert all the elements of another ArrayList array or some other collection to any position of an ArrayList array. The operation of method addAll() is described in Figure 15-1. Method add() can insert only a single element to a specified position of an ArrayList array. There are two versions of both methods. One that adds elements only to the end of an array, and one that is capable of inserting elements in the middle of an array. • To remove or delete elements from an ArrayList array, you can use methods remove() and clear(). When these methods are called, the number of array elements is reduced. There are two versions of method remove(). One version removes the first occurrence of a certain object, while the other removes the element of a certain array position. Method clear() removes all elements from an ArrayList array. • To make copies of an ArrayList array, you can use methods clone() and toArray(). Method clone() creates a shallow copy of an ArrayList array. Method toArray() converts the whole ArrayList array to a conventional array. • To search for a certain element from an ArrayList array, you can use methods indexOf(), lastIndexOf(), and contains(). Method indexOf() can search for an object in an ArrayList array and return its index. The value -1 is returned if the object is not found. Method lastIndexOf() works like indexOf() but it starts the search from the last position of an array and proceeds towards the beginning of the array. Method contains() is a boolean method that can be used to check whether a certain object is in an ArrayList array. Method contains() returns false in the same situation when method indexOf() returns -1. By using the electronic Java documentation you can find more accurate information about the methods of class ArrayList. It is normal that sometimes it is hard to understand how a method of a class works. In such a situation, one possibility is to write a test pr...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online