ush
ush

Reputation: 49

Print a single value in parallel array

Does anyone know how I can output the single corresponding value in a parallel array? Say this is a pair (a | 1, b | 2, c | 3, etc., etc.). If the user enters a then 1 would print.

Upvotes: 1

Views: 168

Answers (2)

Abdelhak
Abdelhak

Reputation: 8387

if you search 'a' retrieve the index of 'a' from array1 and print it

 for (int index  = 0; index  < array1 .length; index ++) {
    if (array1[i].equals("a")){
    System.out.println(array2[index]);
     break;
}
}

Upvotes: 0

Don Scott
Don Scott

Reputation: 3467

Do a traditional indexed linear search, then use the index to get the second value.

Example:

for(int i = 0; i < array1.length; i++){
    if (array1[i].equals(SEARCH_TERM_HERE)){
        return array2[i]; // Or print, etc.
    }
}

Upvotes: 1

Related Questions