Anyone good with java?

Discussion in 'OT Technology' started by Matren, Feb 17, 2008.

  1. Matren

    Matren Well, my name is Jim, but most people call me... J

    Joined:
    Oct 4, 2001
    Messages:
    120,693
    Likes Received:
    64
    Location:
    New Albany, IN
    I has a problem :hsd:

    Code:
    public static void Union(int[] UnionSetOne, int[] UnionSetTwo, int UnionSizeOne, int UnionSizeTwo) //Creates the Union of two sets
    	{
    		int CheckOne, CheckTwo; //Used to check for duplicate numbers in set one and set two respectively
    		int z;
    		ArrayList UnionedSet = new ArrayList(); //Creates the Array List that will store the Union of the two sets
    		ArrayList DupeChecker = new ArrayList(); //Creates an Array List used to check for any duplicate numbers
    		DupeChecker.add(0000);
    		
    		for (int i = 0; i < UnionSizeOne; i++)
    		{
    			CheckOne = 0; //Resets the check with each loop
    			
    			for (int x = 0; x < DupeChecker.size(); x++) //Loop goes through all of the entries in DupeChecker to make sure there are no duplicates being added to the UnionedSet
    			{
    				if (UnionSetOne[i] == DupeChecker.get(x))
    				{
    					CheckOne = 1; //Duplicate is found and thus will not get added to UnionedSet
    				}
    			}
    			
    			if (CheckOne == 0) //Duplicate not found and is added to UnionedSet
    			{
    				UnionedSet.add(UnionSetOne[i]); //Number is added to UnionedSet
    				DupeChecker.add(UnionSetOne[i]); //Number is added to DupeChecker and will now be checked to assure it will not be added to UnionedSet
    			}
    			
    			for (int j = 0; j < UnionSizeTwo; j++)
    			{
    				CheckTwo = 0; //Resets the check with each loop
    				
    				if (UnionSetOne[i] != UnionSetTwo[j]) //Since UnionSetOne[i] was already added to the UnionedSet if it was not a duplicate, this checks to make sure UnionSetOne[i]  and 
    													  //UnionSetTwo[i]  are not the same number
    				{
    					for (int y = 0; y < DupeChecker.size(); y++) //Loop goes through all of the entries in DupeChecker to make sure there are no duplicates being added to the UnionedSet
    					{
    						if (UnionSetTwo[j] == DupeChecker.get(y))
    						{
    							CheckTwo = 1; //Duplicate is found and thus will not get added to UnionedSet
    						}
    					}
    					if (CheckTwo == 0) //Duplicate not found and is added to UnionedSet
    					{
    						UnionedSet.add(UnionSetTwo[j]); //Number is added to UnionedSet
    						DupeChecker.add(UnionSetTwo[j]); //Number is added to DupeChecker and will now be checked to assure it will not be added to UnionedSet
    					}
    				}
    			}
    		}
    		
    		PrintUnionedSet(UnionedSet, UnionedSet.size()); //Calls the method that will print the UnionedSet
    	} //End Method
    For whatever reason, if numbers above ~160 are in either of UnionSetOne or UnionSetTwo, it ends up adding multiple entries of it to the UnionedSet instead of being stopped by the DupeChecker. It works fine with lower numbers, but numbers above a certain range start causing it to do weird things :hsd:

    This is for a math class lab assignment, so I picked something relatively easy to do :o

    Edit: Shit I wish the code box was larger than it is, probably need to C&P that into notepad (I use notepad++ since it has code settings) to see it better :hsd:
     
  2. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    Code:
    List<Integer> list1 = new ArrayList<Integer>();
    for (int i : array1){
    	list1.add(new Integer(i));
    }
    List<Integer> list2 = new ArrayList<Integer>();
    for (int i : array2){
    	list2.add(new Integer(i));
    }
    
    Set<Integer> unionSet = new HashSet<Integer>(list1);
    unionSet.addAll(list2);
    
    System.out.println(unionSet);
    
     
  3. Matren

    Matren Well, my name is Jim, but most people call me... J

    Joined:
    Oct 4, 2001
    Messages:
    120,693
    Likes Received:
    64
    Location:
    New Albany, IN
    Thats a bunch of stuff I've never gone over :o god my java class at my last school sucked balls :o Teacher was nice as fuck, but sucked at actually teaching
     
  4. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    I am looking at it now
     

Share This Page