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
Medium Priority
908 Views
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
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

LVL 16

Expert Comment

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

owenli27 earned 1000 total points
ID: 17917996

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

## Featured Post

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
Course of the Month11 days, 23 hours left to enroll