Results 1 to 8 of 8

Thread: Parallel Arrays

  1. #1
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default 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. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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]);
          }
       }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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. #4
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    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. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  6. #6
    Louis is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default 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 08:17 PM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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. #8
    mwenchong is offline Member
    Join Date
    Nov 2010
    Posts
    14
    Rep Power
    0

    Default

    Yes its an assignment and the three arrays are a requirement

Similar Threads

  1. two parallel arrays
    By Adomini in forum New To Java
    Replies: 12
    Last Post: 09-07-2010, 02:45 AM
  2. Sorting Multiple Parallel Arrays
    By Pyrexkidd in forum New To Java
    Replies: 7
    Last Post: 05-12-2010, 07:34 AM
  3. How to create parallel arrays
    By Roselicious in forum New To Java
    Replies: 6
    Last Post: 04-18-2010, 01:10 PM
  4. I need examples using parallel arrays
    By dangerzone9k in forum New To Java
    Replies: 10
    Last Post: 04-04-2009, 05:11 PM
  5. [SOLVED] Parallel Arrays with Choice ComboBox - need assistance
    By Judoon_Platoon in forum Java Applets
    Replies: 14
    Last Post: 10-01-2008, 10:07 PM

Posting Permissions

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