Results 1 to 10 of 10
  1. #1
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Removing Duplicates.

    Hi I'm fairly new to Java and have hit a small problem.

    I have a 2D array all ready set up. Now I am searching through that array to populate a JOptionPane for a user to select from.

    Ok I can populate the list no problems but what I would like to do is after the String Array has been populated I would like to remove all duplicates first before the list is shown to the user.

    Is it possible and how could I achieve this.

    Thanks

    Code is as follows.
    Java Code:
     String[] code = new String[oceanLink.length];
    
          for (int row = 0; row < oceanLink.length; row++)
          {
             code[row] = oceanLink[row][1];
          }
    
          Object routeCode = JOptionPane.showInputDialog(null, "Please choose a option", "Route Codes", JOptionPane.QUESTION_MESSAGE, null, code, code[1]);
          System.out.println(routeCode);
          String port = routeCode.toString();
          return port;

  2. #2
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    You could merely use a Set.
    Last edited by adz; 12-27-2009 at 04:28 PM.

  3. #3
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Ok I have never used "Set" done a small reading here and there on it but I am unsure where to use it.

    Would I use a .add?

    a little confussed at the moment.

  4. #4
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    yes, an .add(object) is sufficient.

    see java.sun.com/javase/6/docs/api/java/util/HashSet.html#add(E) for example

  5. #5
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    Do something like this:

    Java Code:
      String[] myArray = {"whatever","whatever","whateverx","whateverx"};
      Set<String> noDups = new HashSet<String>(Arrays.asList(myArray));
      myArray = new String[noDups.size()];
      System.arraycopy(noDups.toArray(), 0, myArray, 0, noDups.size());
    Note: Don't know if this is correct since I made it up or if its even the best way to do it. Considering you're just passing the array back to the method in JOptionPane you can simply use the toArray method actually instead of the array copying crap I posted.

    Meh I'm tired so I've just re-read what I posted and decided its well, wrong. You could do this:

    Java Code:
          Set codeSet = new LinkedHashSet<String>();
          for (int row = 0, n = oceanLink.length; row < n; row++)
          {
             codeSet.add(oceanLink[row][1]);
          }
    
          Object routeCode = JOptionPane.showInputDialog(null, "Please choose a option", "Route Codes", JOptionPane.QUESTION_MESSAGE, null, codeSet.toArray(), codeSet.get(1));
          System.out.println(routeCode);
          String port = routeCode.toString();
          return port;
    But er yeah, I'm tired, so apologies if this isn't right too :) Should at least give you the idea, but look up Sets. I suspect if I read this tomorrow I'll wonder why I posted it :/
    Last edited by adz; 12-28-2009 at 01:12 AM.

  6. #6
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    The error in the first example is that although the set contains 2 elements you use arrayCopy method for the two elements. And of course they are equal.

    You propably should use the toArray method to achieve desired results

    Java Code:
    String[] myArray = {"whatever","whatever","whateverx","whateverx"};
    Set<String> noDups = new HashSet<String>(Arrays.asList(myArray)); 
    myArray = noDups.toArray(new String[noDups.size()]);

  7. #7
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    May i interest you in this solution?

    Java Code:
    private static String[] uniqueArguments(String[] arguments) {
    		ArrayList<String> b = new ArrayList<String>();
    		ArrayList<String> c = new ArrayList<String>();
    
    		for (int i = 0; i < arguments.length; i++) {
    			b.add(arguments[i].trim());
    		}
    
    		int busted = 0;
    
    		for (int ix = 0; ix < b.size(); ix++) {
    			busted = 0;
    			for (int j = 0; j < b.size(); j++) {
    				if (b.get(ix).equals(b.get(j))) {
    					busted++;
    				}
    			}
    			if ((busted == 1)) {
    				c.add(b.get(ix));
    			} else if (busted > 1) {
    				b.set(ix, null);
    			}
    		}
    
    		// TODO: I don't understand this statement,
    		// why do we need the "(new String[0]) part???
    
    		logger.info("Done processing -v list. Looks good ..!");
    		return c.toArray(new String[0]);
    	}
    }

  8. #8
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Well this algorithm is slower (quadratic runtime!) than using sets and requires more memory.

    To answer your other question ist the "new String[0]" is used to achive additional type cast (see javadoc of toArray method): the method will return an array of String and not an array of Object.
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  9. #9
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Oh, and the other thing: if you really need that preprocessing and want to use ArrayList then this should do the same as above

    Java Code:
    ArrayList<String> c = new ArrayList<String>();
    
    for (String s : arguments) {
    	String myProcessedString = s.trim();
    	if (!c.contains(myProcessedString)) {
    		c.add(myProcessedString);
    	}
    }
    		
    return c.toArray(new String[c.size()]);
    However i'm a fan of HashSets ;-)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  10. #10
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank you all so very much for the help i used the second method that adz posted thank you again.

    Dash

Similar Threads

  1. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 04:07 PM
  2. No duplicates allowed in Sets
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:33 PM
  3. removing duplicates from arrays
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 06:11 PM
  4. Duplicates
    By Gambit17 in forum New To Java
    Replies: 5
    Last Post: 11-08-2007, 09:56 AM
  5. duplicates in iReport
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 04:42 AM

Posting Permissions

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