简体   繁体   English

Java多维数组排序

[英]Java Multidimensional Array Sort

I have absolutely no idea how to go about this, but I'm using an array for an AI Pathfinding system in Java and I need to sort the values. 我完全不知道如何解决这个问题,但是我在Java中使用数组用于AI Pathfinding系统,我需要对值进行排序。 Here is the Array I'm generating: 这是我正在生成的数组:

int[][] paths = new int[Settings.GAME_COLS][Settings.GAME_ROWS]; // Just creating an Array that has room for every tile in the Game, so the Array can be used like this: paths[x][y]
int tempF = 0;

tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX + 1][aiY - 1] = tempF;
tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX + 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX - 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX - 1][aiY - 1] = tempF;
tempF = Math.abs(10 + (((aiX + 1) - playerX) + (aiY - playerY)));
paths[aiX + 1][aiY    ] = tempF;
tempF = Math.abs(10 + (((aiX - 1) - playerX) + (aiY - playerY)));
paths[aiX - 1][aiY    ] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY + 1) - playerY)));
paths[aiX    ][aiY + 1] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY - 1) - playerY)));
paths[aiX    ][aiY - 1] = tempF;

It all works perfectly find and generates the Array with the information needed, but I need to sort the array based on the "tempF" value added in to the array. 这一切都可以完美地找到并生成具有所需信息的数组,但我需要根据添加到数组中的“tempF”值对数组进行排序。 Is there an easy way to do this? 是否有捷径可寻?

多维数组只不过是一组数组,因此您可以使用常规的Arrays.sort方法,该方法将对您的数组数组进行就地排序。

for(int k=0;k<N;k++)  // loop for relaxation between row-column sorts
{
    for(int i=0;i<N;i++)
    {
       //row-wise sortings(for all rows)
       Arrays.sort(your_array[i]); 
    }

      for ( int c = 0 ; c < N ; c++ )
      {
         for( int d = 0 ; d < N ; d++ )               
         transposed_your_array[d][c] = your_array[c][d];         
      }  

    for(int i=0;i<N;i++)
    {
       //column-wise sortings(for all columns)
       Arrays.sort(transposed_your_array[i]);
    }

    for ( int c = 0 ; c < N ; c++ )
    {
        for( int d = 0 ; d < N ; d++ )               
        your_array[d][c] = transposed_your_array[c][d];         
    }  //getting original array from transpose


}
//There are actually N*N 1-D(N-element)arrays in a N x N matrix
//Every sort on a row-array alters the bounding column-arrays too(N of them)


//a worked example:
//8  3  8
//11 8  3
//6  12 6

//output:
//  this(0,0) is smallest element
//  ^
//  |
//  3  6  8
//  3  8  11
//  6  8  12 ---->this (2,2) is biggest element

//some other examples:
//10 8 8 
//11 4 7 
//9 5 3 
//
//3 5 9 ----> this 9 could be in place of 8 down there but this 
//4 7 10      program sorts rows first then sorts columns
//8 8 11      you can interleave them for better resolution sort(slower)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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