We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Best way to compare two arrays
Page Index Toggle Pages: 1
Best way to compare two arrays? (Read 670 times)
Best way to compare two arrays?
May 6th, 2010, 4:30am
 
Hi, I'm wanting to compare two arrays. One of the arrays will be local; the other a remote one that I load into processing with loadStrings(). I'm wondering the best way to view the difference between the two of them?
After doing a bit of searching I found that you can use an arrayList and using removeAll to find the difference between the both:

Quote:
// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
    add("apple");
    add("orange");
}};

Collection secondList = new ArrayList() {{
    add("apple");
    add("orange");
    add("banana");
    add("strawberry");
}};

// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);

// Remove all elements in firstList from secondList
secondList.removeAll(firstList);

// Show the "after" list
System.out.println("Result: " + secondList);

However I wondered if there was a way to just use arrays, as using an arrayList seems a bit memory intensive for the simple thing I want to do with it.

Thanks for any help,
Tim
Re: Best way to compare two arrays?
Reply #1 - May 6th, 2010, 5:18am
 
kabatwa wrote on May 6th, 2010, 4:30am:
I wondered if there was a way to just use arrays, as using an arrayList seems a bit memory intensive for the simple thing I want to do with it.

Don't worry for memory, unless you have millions of strings, and even then data will take more memory than the ArrayList overhead.
Re: Best way to compare two arrays?
Reply #2 - May 6th, 2010, 6:26am
 
Yes, ok. Makes sense! I guess I'll do it this way then.
Thanks very much!

Tim
Page Index Toggle Pages: 1