1

If I have one array of names, and one array of lap times, how would I link the two arrays so I can sort the times and still keep the associated names? this is what the two arrays look like:

String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};
    int[] times = { 341, 273, 278, 329, 445 };

As of now, I run a selection sort algorithm to get lowest times to highest but i cant figure out how to keep paul connected to 341.

I do NOT want to concatenate the two arrays, I want to be able to sort the times array and then call the name associated with the particular time.

Parth
  • 255
  • 4
  • 13
  • It depends. You either want a `List` of a custom class of objects or you want a `Map`. – jpmc26 Oct 13 '15 at 04:16
  • 1
    Why dont you write a class which will have name and time and you have the array of that class objects. – RP- Oct 13 '15 at 04:16
  • it is a hw assignment, the professor gave us these two arrays as inputs. i dont think he wants us changing this part – Parth Oct 13 '15 at 04:17
  • Possible duplicate of [How can I concatenate two arrays in Java?](http://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) – Anantha Raju C Oct 13 '15 at 04:18

3 Answers3

1

Sort the time array and swap position of element in both the array together, in that way when ever swap happens it will maintain the link between times and names array, here is the program for that, I did sorting in descending Order

public static void main(String[] args){
        String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};
        int[] times = { 341, 273, 278, 329, 445 };
        
        
        for(int outerIndex = 0; outerIndex < times.length; outerIndex++){
            for(int innerIndex = outerIndex+1;  innerIndex < times.length; innerIndex++){
                if(times[outerIndex]<times[innerIndex]){
                    swap(outerIndex, innerIndex, names, times);
                }
            }
        }
    }

Here I am swaping element position in both the arrays:

public static void swap(int outerIndex, int innerIndex, String[] names, int[] times){
        int tempTime;
        String tempName;
        
        tempTime = times[outerIndex];
        tempName = names[outerIndex];
        
        times[outerIndex] = times[innerIndex];
        names[outerIndex] = names[innerIndex];
        
        times[innerIndex] = tempTime;
        names[innerIndex] = tempName;
    }

Input:

String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};

int[] times = { 341, 273, 278, 329, 445 };

Output:

String[] names = {"Pauline", "Paul", "Sam", "Hayden", "Dan"};

int[] times = { 445, 341, 329, 278, 273};

Community
  • 1
  • 1
Ankush soni
  • 1,439
  • 1
  • 15
  • 30
0

Please sort with array room. for example

names[0] = "Paul"
times [0] = 341

something like that

Pune
  • 81
  • 1
  • 9
0

Leave the names and times arrays as-is and use a third array that contains indexes into the other two:

 String[] names = { "Paul", "Dan", "Hayden", "Sam", "Pauline"};
    int[] times = { 341, 273, 278, 329, 445 };
    int[] index = { 0, 1, 2, 3, 4};

Run your sort algorithm on names or times (depending on how you want to sort). When you run the algorithm, access the times array (for example) via the index array:

times[index[x]]; // where x is whatever index you are on during your sort

When you find you need to swap two items, do the swap operation on the index array:

swap(index[0], index[1]);

Once the sorting algorithm is completed, the names and times arrays remain associated via their array positions but if you access them via the index array they will be accessed in sorted order.

p.s. I feel dirty doing your school work for you :)

crchurchey
  • 188
  • 6
  • is the swap operation from java.util.arrays? honestly this homework was more about creating a sorting algorithm. If my prof hadn't already given us this code I was planning on using a 2D array (I did that on the last hw and it worked really well) and sorting on times. – Parth Oct 13 '15 at 04:46
  • also @crchurchey, when you say access the times array via the index array, you meant to type times[index[x]] rather than names[index[x]], right? – Parth Oct 13 '15 at 04:52
  • **swap** is generic; if implemented it just exchanges the items de-referenced by index[0] and index[1]. Many sorting algorithms use something similar. – crchurchey Oct 13 '15 at 04:57
  • So the prof is just having you implement a function? Does it have a return type or are you supposed to just print the arrays in order? – crchurchey Oct 13 '15 at 05:02