- #1
muna580
My professor told me to do everything in this program as asked. But I am a little bit confused.
In the code below, take a look at the part I made in BOLD. Well, first of all, he said to display the elements in vector1. But there is only 1 element in vector1. Well, I undestand its printable, but look at the next step. He said to create vector2 an add all elements in vector1 to vector 2. Basically, I created teh same thing all over again with a diffrent name. What ever I printed out in vector1 will be printed out in vector2.
In the code below, take a look at the part I made in BOLD. Well, first of all, he said to display the elements in vector1. But there is only 1 element in vector1. Well, I undestand its printable, but look at the next step. He said to create vector2 an add all elements in vector1 to vector 2. Basically, I created teh same thing all over again with a diffrent name. What ever I printed out in vector1 will be printed out in vector2.
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;
class CollectionExtraCredit
{
public static void main(String[] arg)
{
Iterator i;
Set s1 = new HashSet();
s1.add("Mary");
// Add the following names - Bernadene, Elizabeth, Gene, Elizabeth, Clara
i = s1.iterator();
System.out.println("HashSet s1 \n" + display(i));
// Create another HashSet object set2
// Add the following members - John, Jerry, Elizabeth
// Add set2 to set1
// Display set2
Set tree1 = new TreeSet();
tree1.add("Mary");
i = tree1.iterator();
// Next, use the same data to create TreeSet tree1 - I did it for you.
// Display the elements of tree1
// Create tree2 and add the elements in tree1 to tree2
// Display tree2
System.out.println("TreeSet s1 \n" + display(i));
List vector1 = new Vector(5);
vector1.add("Mary");
// Thirdly,use the same data again to create Vector vector1 - I did it for you.
// Display the elements of vector1
// Create vector2 and add the elements in vector1 to vector2
// Display vector2
i = vector1.iterator();
System.out.println("Vector vector1 \n" + display(i));
}
static String display(Iterator i)
{
if (!i.hasNext())
return "";
else
return i.next() + "\n" + display(i);
}
}