473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

SetADT help

10
I am trying to print out numbers that intersect between two sets (called set4). I have set 1 and set2. I have tried to use a loop to go through both of them and compare elements then print them out but it didn't work. So basically I am stuck. Here is what I have so far.

package trysets;

import jss2.*;
import java.util.*;

public class IntegerSetTester {
public static void main(String[] args) {
System.out.println("Lab 2: Part II written by William Kluss!");
System.out.println("Creating set1--------------------");
SetADT<Integer> set1 = new ArraySet<Integer> ();
Random rand = new Random();
for (int i = 0; i < 1000; i++) {
set1.add(rand.nextInt(50) + 1);
}
System.out.println("Set 1 is now:\n" + set1);
System.out.println("The size of set 1 is: " + set1.size());
Iterator<Integer> iter = set1.iterator();
Integer firstItem = iter.next();
System.out.println("Removing" + set1.remove(firstItem) + "from set1");
System.out.println("The size of set 1 is now" + set1.size());
System.out.println("Creating set2--------------------");
SetADT<Integer> set2 = new ArraySet<Integer> ();
for (int i = 0; i < 100; i++) {
set2.add(rand.nextInt(10) + 1);
}
System.out.println("Set 2 is now: \n" + set2);
System.out.println("The size of set2 is " + set2.size());
System.out.println("Creating set3--------------------");
SetADT<Integer> set3 = set1.union(set2);
System.out.println("Set 3 is now \n" + set3);
System.out.println("The size of set3 is " + set3.size());
System.out.println("Creating set4--------------------");
SetADT<Integer> set4 = new ArraySet<Integer> ();



}

}
Feb 5 '07 #1
1 1722
Ganon11
3,652 Expert 2GB
You will have to use 2 nested for loops. The first will traverse through every element in set1, and the second will traverse through every element of set2 - compare the two items. Pseudocode will look like this:

Expand|Select|Wrap|Line Numbers
  1. for (from 0 to set1.size()) {
  2.     for (from 0 to set2.size()) {
  3.         Integer one = set1.at(ind1);
  4.         Integer two = set2.at(ind2);
  5.         if (one.value == two.value) {
  6.             set4.add(set1.at(ind1));
  7.             Print out number
  8.         }
  9.     }
  10. }
Feb 6 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
7
by: microsoftboy | last post by:
Hi, Using the following selection criteria, I am able to list the data from mysql db, but I would like to provide the following options for users: Select All Data, Select None. I am not sure if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.