Java Simple Collection Manipulation in Java - Sets, Trees, and Vectors

  • Thread starter Thread starter muna580
  • Start date Start date
AI Thread Summary
The discussion revolves around the manipulation of collections in Java, specifically using Sets, Trees, and Vectors. The initial confusion stems from creating a Vector with only one element, "Mary," and then duplicating it in another Vector, leading to identical outputs. The user clarifies that they successfully implemented the addition of elements to HashSets and TreeSets but are unsure why only "Mary" appears in the Vector outputs. There is also a question about whether the output should be formatted with brackets, indicating a need for clarification on assignment requirements. Overall, the user appears to have followed the instructions correctly but seeks confirmation on expected output and formatting.
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.

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);
}
}
 
Technology news on Phys.org
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);
 
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:
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
		
		s1.add("Bernadene");
		s1.add("Elizabeth");
		s1.add("Gene");
		s1.add("Elizabeth");
		s1.add("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();
		s2.add("John");
		s2.add("Jerry");
		s2.add("Elizabeth");
		
		s2.addAll(s1); 
		
		i = s2.iterator();
		
		System.out.println("HashSet s2 \n" + display(i));		Set tree1 = new TreeSet();
		tree1.add("Mary");
		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();
		tree2.addAll(tree1);
		i = tree2.iterator();
		System.out.println("TreeSet tree2 \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
		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);
		vector2.addAll(vector1);
		i = vector2.iterator();
		System.out.println("Vector vector2 \n" + display(i));
	}
	
	static String display(Iterator i)
	{
		if (!i.hasNext())
			return "";
		else
			return i.next() + "\n" + display(i);
	}
}

This is my output.

HashSet s1
Gene
Bernadene
Clara
Mary
Elizabeth

HashSet s2
Jerry
Bernadene
Gene
Mary
Clara
Elizabeth
John

TreeSet tree1
Mary

TreeSet tree2
Mary

Vector vector1
Mary

Vector vector2
Mary

Press any key to continue...
 
Last edited by a moderator:
Well, it looks correct to me, but you said you expected to get something other than "Mary"? What output should you get?
 
0rthodontist said:
Well, it looks correct to me, but you said you expected to get something other than "Mary"? What output should you get?

One more thing, is there suppose to be any output in bracket? Like for example

[Mary]

is that suppsoe to happen?
 
Did the assignment ask for your output to be in brackets?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
3
Views
3K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
2
Views
4K
Replies
1
Views
3K
Replies
4
Views
3K
Replies
7
Views
8K
Back
Top