Menu
Topics Index
...
`

Compare collection programs


If you need explanation Read this topic

If you need Answer Take test on this topic

Compare collection programs
Compare the following Programs?
import java.util.*;
public class CollectionProgram1
{
    public static void main(String args[])
    {
        List<String> list = new ArrayList<String>();
        java.util.ListIterator<String> listIterator = null;
        list.add("Merit ");
        list.add("Campus ");
        list.add("welcomes ");
        list.add("you ");
        list.add("to ");
        list.add("become ");
        list.add("a ");
        list.add("good ");
        list.add("programmer.");

        listIterator = list.listIterator();

        while(listIterator.hasNext())
        {
            System.out.print(listIterator.next());
        }
    }
}

import java.util.*;
public class CollectionProgram2
{
    public static void main(String a[])
    {
        Vector<String> vector = new Vector<String>();
        Enumeration<String> string = null;
        vector.add("Merit ");
        vector.add("Campus ");
        vector.add("welcomes ");
        vector.add("you ");
        vector.add("to ");
        vector.add("become ");
        vector.add("a ");
        vector.add("good ");
        vector.add("programmer.");

        string = vector.elements();

        while(string.hasMoreElements())
        {
            System.out.print(string.nextElement());
        }
    }
}

A. Both CollectionProgram1 and CollectionProgram2 produce same output. 
B. CollectionProgram1 and CollectionProgram2 produce different output. 
C. CollectionProgram2 will compile and run with out any error,
 but CollectionProgram1 will give compilation error. 
D. CollectionProgram1 will compile and run with out any error,
 but CollectionProgram2 will give compilation error. 
E. Both CollectionProgram1 and CollectionProgram2 will fail to execute. 
Topic: Java Vector

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is A...here we have the ArrayList and Vector concept which are similar in the most of the cases..both implements the List interface and RandomAccess interface..both are growable in the nature and we can add hetrogenious elements to the AL and vector the difference is the the methods in the Vector are Thread-safe and AL methods are not thred safe and vecctor is legacy class and AL is new class..for AL we can get the synchronized version by using the Collections.synchronizedList()..
Execution:
Case 1:
in this program we create the object for the AL which can store the objects of the Strings and next we create the reference for listIterator and next we added some strings objects to the list
using the we get the listIterator() object by invoking the listIterator(); so using the we printed the elements of the AL
Case 2:
in this program we create the object for the Vector which can store the objects of the Strings and next we create the reference for Enumaration and next we added some strings objects to the Vector,
by invoking the elements() we got the Enumaration which is used to iterate through the Vector by using that we print the elements

listIterator will applicable for the only the classes which implements the List interface and Enumaration is an legacy class..by using this we can not  remove elements and we can not naviagate

Posted by Uday Kumar    2015-02-25 16:17:12


Answer is a

Posted by Shashanka Mogaliraju    2015-02-25 18:17:19


ANS IS A (Both programs produce same output.)

--Here, the concept of araylist and list is used.

--Both arraylist and vector are dymic I.e. can grow depending on no of elements inserted.Both have hasmoreelements() and nextelement() method to display the elements of collection.

--In both the collections string elements are inserted every element follows space.

--Iterator and enumeration are used to display the elements of list and vector to user.

So boh programs collction1 and collection2 produce he same output.

Posted by Mânïshå Mùlchåndânï    2015-02-25 18:31:12


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-26 03:43:53


© meritcampus 2019

All Rights Reserved.

Open In App