Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array comparison algorithm of different sized arrays

Posted on 2006-11-10
2
Medium Priority
?
908 Views
Last Modified: 2008-01-09
Hello all,

I seem to have a brain freeze going and I sure could use some help before I do something drastic (you know I never really liked that guy in the next cubicle)!

Assume I have 2 arrays.  Array 1 is a list of possible string values:

final String colors[] = {"black", "brown", "red", "orange", "yellow",
                            "green", "blue", "violet","grey", "white"};

Array 2 is a list of 3 colors chosen by a user:

final String userColors[] = {"red", "yellow", "white"};

How can I compare the two arrays to make sure that userColors[] only has values that can be found in colors[]?

Thanks,
Brian
0
Comment
Question by:bnblazer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 17917975
In pseudo code it would be something like:

set usercolorsvalid to true
for all user colors
     set incolor to false
     for all colors
          if current user color equals current color
               set incolor to true
               break
     if not incolor set usercolorsvalid to false

at the end usercolorsvalid will indicate whether all user colors are in the colors array or not. Basically, you go through them one by one, and check if there is an entry for them in the colors array
0
 
LVL 9

Accepted Solution

by:
owenli27 earned 1000 total points
ID: 17917996

if ((Arrays.asList(colors)).containsAll(Arrays.asList(userColors) ))
{
....
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question