简体   繁体   中英

Java How to print the original index of a sorted 2d array?

suppose I have a 5X1 2D array [2,3,1,4,5], so the index would be [[0,0],[1,0],[2,0],[3,0],[4,0]] still, but how to have the original index stay in the value? so then i can print out the index after sorting the value (if that make sense), example

after sorting the value will be [1,2,3,4,5], but the index would be [2,0][0,0][1,0][3,0][4,0] how exactly do you manipulate it? Thanks in advance, help will be much appreciated!

{
 int[][] array = new int[5][1];

 array = {{2,3,1,4,5}};
 //at this point the array index will be [[0,0],[1,0],[2,0],[3,0],[4,0]]

 Arrays.sort(array);
 //i want the index to stay within the value after sorting...

} 

Odd problem. I'd restructure the code entirely to use my own custom object that held not only the value, but also the original location.

The other way would be to write your own sort, but as you sort, keep a second array that has the original locations, and everything you do to the first array, you mirror on the second array.

A third way: if the values of the array are guaranteed to be unique, you could first duplicate the array. Call one origArray and one sortedArray. Then, when looking at an item in sortedArray, find it in origArray, and that will tell you where it used to be. But this depends on the values being unique, and that's a poor assumption.

You may want to re-edit your question. Your syntax is incorrect for you array allocation.

It should be as follows.

  v = new int[][]{{2},{3},{1},{4},{5}};

And once you change it, you can't use sort that way.

To solve your problem I would create a map which maps the index to its original value. I also changed the array allocation as discussed previously.

      int[][] vals;
      vals = new int[][] { { 12, 31, 21, 75, 15
            }
      };

      Map<Integer, Integer> indices =
            IntStream.range(0, vals[0].length).boxed().collect(
                  Collectors.toMap(i -> i, i -> vals[0][i]));

      indices.forEach((k, v) -> System.out.println(k + " => " + v));

      Arrays.sort(vals[0]);

      System.out.println(Arrays.toString(vals[0]));

This prints

0 => 12
1 => 31
2 => 21
3 => 75
4 => 15
[12, 15, 21, 31, 75]

If you want to map value to original index, the just reverse the parameters to Collector

    Collectors.toMap(i -> vals[0][i], i->i)

suppose I have a 5X1 2D array [2,3,1,4,5], so the index would be [[0,0],[1,0],[2,0],[3,0],[4,0]] still, but how to have the original index stay in the value? so then i can print out the index after sorting the value (if that make sense), example

after sorting the value will be [1,2,3,4,5], but the index would be [2,0][0,0][1,0][3,0][4,0] how exactly do you manipulate it? Thanks in advance, help will be much appreciated!

{
 int[][] array = new int[5][1];

 array = {{2,3,1,4,5}};
 //at this point the array index will be [[0,0],[1,0],[2,0],[3,0],[4,0]]

 Arrays.sort(array);
 //i want the index to stay within the value after sorting...

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM