Solved

Array comparison algorithm of different sized arrays

Posted on 2006-11-10
2
898 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
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 250 total points
ID: 17917996

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 71
base64 decode encode 12 122
windows explorer path to command prompt 5 33
java stored proc example 9 23
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now