Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 6th, 2006, 03:25 PM
derbys derbys is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 1 derbys User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 12 sec
Reputation Power: 0
Java newbie help.

Hey, im in need of a little help with a little piece that i need to do and i dont especially get how to do it. What i want to do is write a method that takes two one-dimensional arrays of strings and determine whether or not both arrays contain the same words. For example, the following two arrays contain the same words:

cast static thread make void import

void make import cast thread static

Im struggling of how i would go about doing tis and if anyone has any ideas id be greatful.

Cheers.

Reply With Quote
  #2  
Old November 6th, 2006, 05:32 PM
fizker fizker is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: denmark
Posts: 42 fizker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 18 m 18 sec
Reputation Power: 5
Send a message via MSN to fizker
The answer seems to be to just iterate through each array, and compare all elements to each other.

For example (I know, ugly complexity, but it gets the job done):
Code:
public void printDuplicates(String[] arr1, String[] arr2) {
    for(int i = 0; i < arr1.length; i++) {
        for(int j = 0; j < arr2.length; j++) {
            /* remember to use the right indexer, or you will get an indexOutOfBounds exception */
            if(arr1[i].equals(arr2[j])) {
                System.out.println("Duplicate found: "+arr1[i]);
            }
        }
    }
}
Here I print all the duplicates, but it should be simple to alter the code to do whatever you need .
__________________
Benjamin Horsleben
horsleben.com/benjamin
Don't blame malice for what stupidity can explain

Reply With Quote
  #3  
Old November 7th, 2006, 03:38 PM
colton22's Avatar
colton22 colton22 is offline
\ ^_^ / - Moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: near chicago, Illinois
Posts: 473 colton22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 1 h 19 m 14 sec
Reputation Power: 3
Send a message via AIM to colton22 Send a message via MSN to colton22 Send a message via Yahoo to colton22
if you get that error just use...
try-catch to alter and keep going

colton22

Reply With Quote
  #4  
Old November 7th, 2006, 10:32 PM
daniel_g daniel_g is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 60 daniel_g User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 3 m 50 sec
Reputation Power: 3
Added a count to fizker's code to see if all the word on arr1 are on arr2:
Code:
public void printDuplicates(String[] arr1, String[] arr2) {
    int count = 0;
    for(int i = 0; i < arr1.length; i++) {
        for(int j = 0; j < arr2.length; j++) {
            /* remember to use the right indexer, or you will get an indexOutOfBounds exception */
            if(arr1[i].equals(arr2[j])) {
                System.out.println("Duplicate found: "+arr1[i]);
                count++;
            }// end if
        }// end for
    }// end for
   if(count == arr1.length)
       System.out.println("All words on array1 are on array2");
}

Reply With Quote
  #5  
Old December 28th, 2006, 01:29 PM
FUBAR FUBAR is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 2 FUBAR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 59 sec
Reputation Power: 0
to make the method more efficient, in terms of doing as little checking whenever possible, I'd approach it something like this...

Code:
public boolean matches(String[] arr1, String[] arr2) {
    if (arr1.length == arr2.length) {
        for (int i = 0; i < arr1.length; i++) {
            boolean foundMatch = false;
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i].equals(arr2[j])) {
                    foundMatch = true;
                    break;
                }
            }
            if (!foundMatch) {
                return false;
            }
        }
        return true;
    }
    return false;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Java newbie help.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT