Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple Collection Stuff

  1. Nov 8, 2006 #1
    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.

  2. jcsd
  3. Nov 8, 2006 #2


    User Avatar
    Science Advisor

    You haven't declared vector2. Do so, and then you need to use a copy function. e.g. Vector vector2 = vector1.clone();

    Or, and this might be better practice for you, is to make your own clone function that creates a new vector and iterates through vector1, adding all the objects in vector1 to the new vector, and returns the new vector. Then you can say Vector vector2 = clone(vector2);
  4. Nov 8, 2006 #3
    This is what I did. But I am not sure if it is correct becaue for TreeSet, and Vector, only get Mary as the output. Am I suppoe to add something else?

    Code (Text):

    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();

            // 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 s2 = new HashSet();
            i = s2.iterator();
            System.out.println("HashSet s2 \n" + display(i));

            Set tree1 = new TreeSet();
            i = tree1.iterator();
            System.out.println("TreeSet tree1 \n" + display(i));
            // 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
            Set tree2 = new TreeSet();
            i = tree2.iterator();
            System.out.println("TreeSet tree2 \n" + display(i));

            List vector1 = new Vector(5);
            // Thirdly,use the same data again to create Vector vector1 - I did it for you.
            // Display the elements of vector1
            i = vector1.iterator();
            System.out.println("Vector vector1 \n" + display(i));

            // Create vector2 and add the elements in vector1 to vector2
            // Display vector2
            List vector2 = new Vector(5);
            i = vector2.iterator();
            System.out.println("Vector vector2 \n" + display(i));
        static String display(Iterator i)
            if (!i.hasNext())
                return "";
                return i.next() + "\n" + display(i);
    This is my output.

    Last edited by a moderator: Nov 8, 2006
  5. Nov 8, 2006 #4


    User Avatar
    Science Advisor

    Well, it looks correct to me, but you said you expected to get something other than "Mary"? What output should you get?
  6. Nov 8, 2006 #5
    One more thing, is there suppose to be any output in bracket? Like for example


    is that suppsoe to happen?
  7. Nov 8, 2006 #6


    User Avatar
    Science Advisor

    Did the assignment ask for your output to be in brackets?
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?