Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-12-2007, 09:53 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
removing duplicates from arrays
I have a String array with values in it. Their are duplicates in it. I want to duplicates from the array but don't know how to do it. I understand that I can do this using for loop - but then I have to use nested for loops and I think its not a good way of doing it.

If someone knows a better way, please advice.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-13-2007, 01:02 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Use methods.
Code:
public class RemoveDuplicates { static String[] strs = { "arrays", "java", "manipulate", "java", "simple", "arrays", "manipulate", "java" }; public static void main(String[] args) { print(strs, "strs"); int num = getNumUniqueValues(); String[] uniqueValues = new String[num]; for(int j = 0, k = 0; j < strs.length; j++) { if(!containsValue(uniqueValues, strs[j])) uniqueValues[k++] = strs[j]; } print(uniqueValues, "uniqueValues"); } private static int getNumUniqueValues() { String[] values = new String[strs.length]; int count = 0; for(int j = 0; j < strs.length; j++) { if(!containsValue(values, strs[j])) values[count++] = strs[j]; } return count; } private static boolean containsValue(String[] array, String target) { for(int j = 0; j < array.length; j++) { if(array[j] != null && array[j].equals(target)) return true; } return false; } private static void print(String[] array, String s) { System.out.println(s + ":"); for(int j = 0; j < array.length; j++) { System.out.print(array[j]); if(j < array.length-1) System.out.print(", "); } System.out.println(); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-13-2007, 10:11 AM
Senior Member
 
Join Date: Nov 2007
Posts: 111
bugger is on a distinguished road
Thanks hardwired. I appreciate this.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-13-2007, 07:11 PM
Member
 
Join Date: Nov 2007
Posts: 7
Gambit17 is on a distinguished road
yo bugger had a similar problem a week a ago if you want to know how i sorted it out just say so on this thread. yip struggled with it for awhile but gor it in the end
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
No duplicates allowed in Sets Java Tip Java Tips 0 01-21-2008 05:33 PM
image removing Triss New To Java 3 01-20-2008 09:27 PM
Removing characters kDude New To Java 3 12-03-2007 03:38 AM
Duplicates Gambit17 New To Java 5 11-08-2007 10:56 AM
duplicates in iReport Heather Advanced Java 1 07-05-2007 05:42 AM


All times are GMT +3. The time now is 09:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org