Java排序2D字符串数组不再起作用

时间:2013-03-08 21:06:09

标签: java

好的所以我正在研究一种2D数组的排序方式,其中一个维度有一个字符串,另一个维度有一个int(为方便起见,存储为字符串)我到处寻找一个关于如何排序的解决方案数组来自firstArray [1]的数据同时被移动(它的索引是一个子移动到:)作为firstArray [0]

使用此

实现了此效果
Arrays.sort(fps, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);
            }
        });

现在我遇到麻烦了。我将在这里逐步完成代码,如果你能找到问题,请告诉我们。

首先我有阵列:

String[][] fps = new String[2][15];
Arrays.fill(fps[0], "empty");
Arrays.fill(fps[1], "0");

第二,我在数组中加入了程序的另一部分给我的一些东西,对于这个例子,使用垃圾值:

fps[0][0] = "Java";
fps[1][0] = "1";

fps[0][1] = "C++";
fps[1][1] = "14";

fps[0][2] = "C#";
fps[1][2] = "21";

fps[0][3] = "Python";
fps[1][3] = "9001";

现在我将调用上面的排序命令(请注意,这些值不会完全填充数组,有些区域没有新数据。)

Arrays.sort(fps, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);
            }
        });

现在我们对数组进行了排序,我想在2D数组中搜索一个值,所以我使用Arrays.search来查找查询所在的bin。

int searchIndex = Arrays.binarySearch(fps[0], "Java");
System.out.println(searchIndex);

这就是代码,我认为我已经将问题分离为排序部分无法正常工作。如果您有任何疑问,请在评论中发布。同样,如果你能解决这个令人费解的问题,我很乐意听到它!


PS:要明确我有这个工作,然后我关闭我的lappy,下次我启动(从那时起)它没有用。

PPS:根据要求输出:

当前输出:

-16
FPS:
    0 ---- No FPS For you!
    1 ---- Only one FPS
    2 ---- Only two FPS
    3 ---- Only three FPS
    4 ---- Only four FPS
    5 ---- Only five FPS
    6 ---- Only six FPS
    7 ---- Only seven FPS
    8 ---- Only eight FPS
    9 ---- Only nine FPS
    1 ---- Blah!

预期/希望输出:

-16
FPS:
    1 ---- Blah!
    0 ---- No FPS For you!
    8 ---- Only eight FPS
    5 ---- Only five FPS
    4 ---- Only four FPS
    9 ---- Only nine FPS
    1 ---- Only one FPS
    7 ---- Only seven FPS
    6 ---- Only six FPS
    3 ---- Only three FPS
    2 ---- Only two FPS

PPPS:如果您想查看我目前正在使用的代码:

import java.util.*;

public class Test
{
  public static void main (String [] args)
  {

    String[][] fps = new String[2][15];
    Arrays.fill(fps[0], "empty");//Fill up all the spaces so the sort and the search dont crap out
    Arrays.fill(fps[1], "0"); 


    //fps[ROW][COLOUMN] = Value + "";
    //fps[ROW][COLOUMN] = Value Instances + "";

    fps[0][0] = "No FPS For you!";
    fps[1][0] = 0 + "";

    fps[0][1] = "Only one FPS";
    fps[1][1] = 1 + "";

    fps[0][2] = "Only two FPS";
    fps[1][2] = 2 + "";

    fps[0][3] = "Only three FPS";
    fps[1][3] = 3 + "";

    fps[0][4] = "Only four FPS";
    fps[1][4] = 4 + "";

    fps[0][5] = "Only five FPS";
    fps[1][5] = 5 + "";

    fps[0][6] = "Only six FPS";
    fps[1][6] = 6 + "";

    fps[0][7] = "Only seven FPS";
    fps[1][7] = 7 + "";

    fps[0][8] = "Only eight FPS";
    fps[1][8] = 8 + "";

    fps[0][9] = "Only nine FPS";
    fps[1][9] = 9 + "";
    /* FUMBLE WITH ARRAY AFTER THIS LINE ONLY!*/

    //Things to have inputed into the method:
    //currentValue (from the line)
    //currentVariable (so we know what the name of the array we're dealing with is named)

    String currentValue = "Blah!"; //This is the value that will be searched for in the array, if found its child int is incremented by one, if not found it is added to the array.


    //Do a binary sort then search in the fps[0] side of things, makesure that the [1] are carried with the [0] changes.

    Arrays.sort(fps, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);
            }
        });

    int searchIndex = Arrays.binarySearch(fps[0], currentValue); //Get the index of the current search value
    System.out.println(searchIndex); //  <-- Returns a neg number each time which shows that the sorting is not working correctly, and therefore the search is being thrown off... need to somehow fix.

    if(searchIndex >= 0)// If the value is found
    {
      fps[0][searchIndex] = (Integer.parseInt(fps[0][searchIndex]) + 1) + "";  //Add one instance to the value

    } //end if
    else //Otherwise find the next open value spot and change it to the current search query (and assign its instances to 1
    {

      for(int i = 0; i < fps[1].length ; i++)
      {
        if(fps[1][i].equals("empty"))
        {
          fps[1][i] = currentValue;
          fps[0][i] = 1 + "";
          i = fps[1].length; //force the for loop to exit

          Arrays.sort(fps, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);
            }
        }); //end Arrays.sort
        }//end if
      }//end for
    }//end else


    //... Print array in rectangular form
    System.out.println("FPS:");

    for (int i =0; (!(fps[1][i].equals("empty")) ) ; i++)
    {
      System.out.println("\t" + fps[0][i] + " ---- " + fps[1][i] );


    }//end for
  }//end main
}//end class

3 个答案:

答案 0 :(得分:1)

我相信你的指数倒退了。您正在排序fpsfps只有2个元素正在排序。我相信你正试图对15个元素进行排序。如果您反转索引,我相信您将获得所需的排序。

String[][] fps = new String[15][2];

在这种情况下,您可以考虑一组对象而不是二维数组。它似乎是一个更合理的结构,可以避免这种混淆。

答案 1 :(得分:0)

  

你有关于这些对象的任何资源(当在与此

类似的实例中使用时)

有关如何在使用自定义对象时执行此操作的示例,请参阅:Sorting using Comparator- Descending order (User defined classes)

  

其中一个维度有一个字符串,另一个维有一个int(为方便起见,存储为字符串)

它不方便,因为排序数字的字符串表示与排序数字不同。使用自定义对象将允许您正确存储数据,以便进行正确的排序。

答案 2 :(得分:0)

this Answer中指出的问题外,还有一个问题:

int searchIndex = Arrays.binarySearch(fps[0], "Java");

由于您使用自定义比较器进行排序,因此需要使用相同的自定义比较器来执行二进制搜索。使用binarySearch(T[] a, T key, Comparator<? super T> c)。 (如果您使用2-arg版本,则应该得到例外,因为String[]未实现Comparable。)