In: Computer Science
Find a method of ArrayList that is not in the List interface, specifically a method that trims the internal array down to fit exactly. A Google search for this did work, but the JDK API of course is the definitive source.
TestArrayList1
import java.util.ArrayList;
public class TestArrayList {
public static void main( String [ ] args )
{
ArrayList<String> array = new ArrayList<String>();
array.add("apple");
array.add("banana");
// Loop through the collection by index
for ( int i = 0; i < array.size( ); i++ ) {
System.out.println( array.get( i ) );
}
// Using the fact that Collections are Iterable
for (String s: array) {
System.out.println(s);
}
}
}
TestArrayList2
import java.util.ArrayList;
import java.util.Iterator;
public class TestArrayList2 {
public static void main( String [ ] args )
{
ArrayList<String> array = new ArrayList<String>();
array.add("apple");
array.add("banana");
// Loop through the collection by using an Iterator
Iterator<String> itr = array.iterator();
while (itr.hasNext()) {
System.out.println(itr.next() );
}
}
}
TestArrayList3
import java.util.ArrayList;
import java.util.ListIterator;
public class TestArrayList3 {
public static void main( String [ ] args )
{
ArrayList<String> array = new ArrayList<String>();
array.add("apple");
array.add("banana");
// Loop through the collection by using a ListIterator
ListIterator<String> itr = array.listIterator();
while (itr.hasNext()) {
System.out.println(itr.next() );
}
// going backwards:
ListIterator<String> itr2 = array.listIterator(array.size());
while (itr2.hasPrevious()) {
System.out.println(itr2.previous() );
}
// The listIterator is still alive--
System.out.println(itr2.next());
}
}
TestArrayList4
import java.util.ArrayList;
import java.util.ListIterator;
class TestArrayList4
{
public static void main( String [ ] args )
{
ArrayList<Integer> lst = new ArrayList<Integer>( );
lst.add( 2 );
lst.add( 4 );
ListIterator<Integer> itr1 = lst.listIterator( 0 );
System.out.print( "Forward: " );
while( itr1.hasNext( ) )
System.out.print( itr1.next( ) + " " );
System.out.println( );
System.out.print( "Backward: " );
while( itr1.hasPrevious( ) )
System.out.print( itr1.previous( ) + " " );
System.out.println( );
System.out.print( "Backward: " );
ListIterator<Integer> itr2 =
lst.listIterator( lst.size( ) );
while( itr2.hasPrevious( ) )
System.out.print( itr2.previous( ) + " " );
System.out.println( );
System.out.print( "Forward: ");
for( Integer x : lst )
System.out.print( x + " " );
System.out.println( );
}
}
a) Give the method header for the method
Answer: public void trimToSize()
b) Add a call to this method to TestArrayList.java.
Answer: Please see the updated code below:
import java.util.ArrayList;
public class TestArrayList {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("apple");
array.add("banana");
//trimming to size
array.trimToSize();
// Loop through the collection by index
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
// Using the fact that Collections are Iterable
for (String s : array) {
System.out.println(s);
}
}
}
Now change the line creating the ArrayList to use type List<String>for its variable (List <String>array = ...), and add the needed import for List. What happens when you try to compile this new version? and c) Explain why the compilation failed in this case
Answer: The updated code is below, which will not compile because trimToSize method is specific to ArrayList only, List interface does not have declaration for this method, and when you change the type of array object to List from ArrayList, the array object can call only those methods defined in List interface without typecast.
import java.util.ArrayList;
import java.util.List;
public class TestArrayList {
public static void main(String[] args) {
List<String> array = new ArrayList<String>();
array.add("apple");
array.add("banana");
//trimming to size
array.trimToSize();
// Loop through the collection by index
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
// Using the fact that Collections are Iterable
for (String s : array) {
System.out.println(s);
}
}
}
d) Show that you can leave the type of array as List for most of the program (which is good programming practice, since most of it only needs a List) and still call this special method by "downcasting" array to ArrayList just for this one call.
Answer: Please see the updated code below:
Note: The line: ((ArrayList<String>) array).trimToSize(); is where the downcast happens.
import java.util.ArrayList;
import java.util.List;
public class TestArrayList {
public static void main(String[] args) {
List<String> array = new ArrayList<String>();
array.add("apple");
array.add("banana");
// trimming to size
((ArrayList<String>) array).trimToSize();
// Loop through the collection by index
for (int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
// Using the fact that Collections are Iterable
for (String s : array) {
System.out.println(s);
}
}
}
Note: I have no idea why you have provided different versions of TestArrayList classes. The changes specified in this question can be applied to any of those classes. Since the class name is TestArrayList, I have only made changes to the first version you attached. Feel free to experiment with other versions and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks