# Thread: Set of Integers

1. Member
Join Date
Jan 2010
Posts
23
Rep Power
0

## Set of Integers

Hello there! H0w can i make a java program using Queue that would print a set of Integers..for example; Union.,.
Qa = 123
Qb = 456

output = 123456

Intersection

Qa = 123
Bb = 135

output = 13

Cartesian

Qa = {1,2}
Qb = {3,4}

output = 1, 3, 1 ,4 ,2 ,3, 2, 4

I can't visualized this. Can somebody help me or just give me a sample program? Thank you.:)

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
Why use a Queue to represent a set when a Set is available?

Cartesian products involve creating a new data type. If you are only printing the product and not manipulating it just use nested for loops.

3. Member
Join Date
Jan 2010
Posts
21
Rep Power
0

## Here is the answer for you

Here is the coding.... It may be useful to you.....

4. Originally Posted by rummy
Here is the coding.... It may be useful to you.....
That code won't work; try the union of { 1, 2, 3 } and { 2, 3, 4 } for example.

kind regards,

Jos

5. Member
Join Date
Jan 2010
Posts
23
Rep Power
0
Oh, thank you for the code. Highly appreciated. But I have another one. How to implement an array using Queue? Thanks

6. Member
Join Date
Jan 2010
Posts
21
Rep Power
0

## Sorry..... I think this one is correct...

Hey .... Sorry i made a mistake.... Please check it out .... Here to remove the duplicates i use TreeSet....

7. Member
Join Date
Jan 2010
Posts
23
Rep Power
0
Thank you again. But in my program I have 3 choices. 1.Union 2.Intersection. 3. Cartesian.
When I compiled the program the compiler would ask me the 3 choices. If I entered 1 it will switch to Union then I will input depend how many numbers I'd like to input. For ex: Qa is 123, Qb is 456. output is = 123456. But there is an exception. If entered the same element in the Queue u can display one. For ex: QA- 123, QB 234. Output = 1234..it cannot be 122334. Thank you so much. Appreciated it.

8. Originally Posted by rsjava24
Thank you again. But in my program I have 3 choices. 1.Union 2.Intersection. 3. Cartesian.
When I compiled the program the compiler would ask me the 3 choices. If I entered 1 it will switch to Union then I will input depend how many numbers I'd like to input. For ex: Qa is 123, Qb is 456. output is = 123456. But there is an exception. If entered the same element in the Queue u can display one. For ex: QA- 123, QB 234. Output = 1234..it cannot be 122334. Thank you so much. Appreciated it.
Read the previous replies instead of copying and pasting code without thinking: rummy's original code doesn't work as I wrote before. It allows duplicates as you have noticed.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•