1. Member
Join Date
Nov 2010
Posts
14
Rep Power
0

## Parallel Arrays

I want to know how to get the corresponding indexes of two arrays.

arr1 = { a, b, c, d}
arr2 = { 5, 6, 2, 9}

The position of the Element 'b' is 1 and the position of element '6' is 1. Knowing that Java count from 0. So I want to know how to compare or relate the two indexes and not the the elements.

Thanks

2. Senior Member
Join Date
May 2010
Posts
436
Rep Power
7

But regardless, you can loop through both easily, for example
Java Code:
```   public static void main(String[] args) {
char[] arr1 = {'a', 'b', 'c', 'd'};
int[] arr2 = {5, 6, 2, 9};

System.out.printf("%-10s %-10s %-10s%n", "index", "arr1", "arr2");
for (int i = 0; i < arr1.length; i++) {
System.out.printf("%-10d %-10c %-10d%n", i, arr1[i], arr2[i]);
}
}```

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Do you mean you want to supply 'b' and get back 6?

If so a Map is just the thing:

Java Code:
```import java.util.HashMap;
import java.util.Map;

public class MapEg {
public static void main(String[] args){
Map<String,Integer> data = new HashMap<String,Integer>();

data.put("a", 5);
data.put("b", 6);
data.put("c", 2);
data.put("d", 9);

System.out.println("b corresponds to " + data.get("b"));
}
}```

---------------------------

Less than just the thing would be to write a method that finds the index of an element within an array. (A for loop which checks each one...) Then once you have indices you can compare and relate them as you will.

4. Member
Join Date
Nov 2010
Posts
14
Rep Power
0
My actual program should read the fastest runner in a marathon. Information comes from three arrays. One for Names, one for Minutes and the last one for seconds. I wrote the following code

public class marathon {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String[] names = {"Elena", "Demertiris", "Katerina", "Phil", "Matteo",
"alexis", "Emmanuel", "Amir", "Daniel", "Neda"};

int[] timesMin = {341, 273, 334, 445, 402, 388, 273, 334, 399, 343};

int[] timeSec = {25, 30, 37, 44, 56, 05, 12, 51, 14, 27};
int i, j, k;
// k = 0;
System.out.println("NAME TIME(Mins & Secs)");

for ( i = 0; i < names.length; i++) {
int count = i + 1;
System.out.print(count + " ");
System.out.println(names[i] + " " + timesMin[i] + "m:" + timeSec[i] + "s");
}
System.out.println();
int minimum = timesMin[0];
int min = timeSec[0];
boolean fastest = true;

for ( j = 0; j < names.length; j++) {
if (timesMin[j] < minimum && timeSec[j] < min ) {
k = j + 1;
minimum = timesMin[j]; // new minimum

min =timeSec[j];

System.out.println("The Fastest Runner is Number: " + k);

System.out.println(names[j] + " " + minimum + "m:" + timeSec[j] + "s");

}
}

}

}

But when I change minutes I don`t get the desired result.

5. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
8
Is this a school assignment? If so, are the three arrays a requirement? Parallel arrays aren't very pleasant to deal with, it would be much better to make a Result class that holds the name, minutes and seconds, that way you can hold all the data in just one array.

6. Member
Join Date
Nov 2010
Posts
9
Rep Power
0

## I'm begineer so i simply write this code. Got error help me do some correction thanks

Are you try to tell us is use the element to check the index is same or not right???

import java.util.Scanner;
/**
*
* @author Louis
*/
public class testing {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); //For scanner
String[] arr1 = {"a","b","c","d"}; //array list
int[] arr2 = {5,6,2,9};
int first=0,second=0; // use to temporary store index

//let user want to compare which index
String a = scan.next();
int b = scan.nextInt();

for(int x=0;x<arr2.length;x++){
if(a.equals(arr1[x])){
first = x; // the index of arr1
}
else{
System.out.println("Out of range!!");
}
if(b == x){
second = x; // the index of arr2
}
else{
System.out.println("Out of range!!");
}
}
//here is to check the index is same or not.. same will be true !!
if(first == second){
}
else{
}
System.out.println("The arr1 "+first+ " and arr2 "+second+" same index :"+answer);

}
}

** I'm begineer for the java .. if got problem help me correction post it here.. Thank you^^
Last edited by Louis; 11-16-2010 at 08:17 PM.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Java Code:
```int minimum = timesMin[0];
int min = timeSec[0];
boolean fastest = true;

for ( j = 0; j < names.length; j++) {
if (timesMin[j] < minimum && timeSec[j] < min ) {
k = j + 1;
minimum = timesMin[j]; // new minimum

min =timeSec[j];

System.out.println("The Fastest Runner is Number: " + k);

System.out.println(names[j] + " " + minimum + "m:" + timeSec[j] + "s");

}
}```

Here you seem to be trying to find the index of the runner.

(1) The condition of the if statement isn't correct. You are saying "if j's minutes are less than the fastest so far AND j's seconds are less than the fastest so far" you should be saying "if j's minutes are less than the fastest so far OR (they are equal AND j's seconds are less than the fastest so far)"

(2) You don't know who the fastest runner is until after the for loop has finished. So move the System.out.println() stuff out of the for loop.

(3) What is k all about? If you don't have any clear intention for it at the moment, remove it from the code.

8. Member
Join Date
Nov 2010
Posts
14
Rep Power
0
Yes its an assignment and the three arrays are a requirement

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•