Solved

Array comparison algorithm of different sized arrays

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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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:

740 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