Results 1 to 8 of 8
Thread: Parallel Arrays
- 11-15-2010, 11:17 PM #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
- 11-16-2010, 02:47 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
What are you asking?
Your code doesn't compile.
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]); } }
- 11-16-2010, 03:17 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 11-16-2010, 03:29 PM #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.
- 11-16-2010, 03:41 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 11-16-2010, 04:23 PM #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
System.out.println("Please enter arr1 :");
String a = scan.next();
System.out.println("Please enter arr2 :");
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 !!
boolean answer=false;
if(first == second){
answer = true;
}
else{
answer=false;
}
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 07:17 PM.
- 11-16-2010, 07:07 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.
- 11-17-2010, 12:20 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
two parallel arrays
By Adomini in forum New To JavaReplies: 12Last Post: 09-07-2010, 01:45 AM -
Sorting Multiple Parallel Arrays
By Pyrexkidd in forum New To JavaReplies: 7Last Post: 05-12-2010, 06:34 AM -
How to create parallel arrays
By Roselicious in forum New To JavaReplies: 6Last Post: 04-18-2010, 12:10 PM -
I need examples using parallel arrays
By dangerzone9k in forum New To JavaReplies: 10Last Post: 04-04-2009, 04:11 PM -
[SOLVED] Parallel Arrays with Choice ComboBox - need assistance
By Judoon_Platoon in forum Java AppletsReplies: 14Last Post: 10-01-2008, 09:07 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks