Java排序2D字符串数组不再工作
好的,我正在研究一种对2D数组进行排序的方法,其中一个维度有一个字符串,另一个维度有一个int(为了方便起见存储为字符串)。我到处寻找一种解决方案,如何对数组进行排序,使来自firstArray[1]的数据同时移动(其索引是移动的子项:)作为第一个数组[0] 这种效果是通过使用Java排序2D字符串数组不再工作,java,Java,好的,我正在研究一种对2D数组进行排序的方法,其中一个维度有一个字符串,另一个维度有一个int(为了方便起见存储为字符串)。我到处寻找一种解决方案,如何对数组进行排序,使来自firstArray[1]的数据同时移动(其索引是移动的子项:)作为第一个数组[0] 这种效果是通过使用 Arrays.sort(fps, new Comparator<String[]>() { @Override public int compare(final
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);
}
});
其次,我用程序的另一部分提供给我的一些东西填充数组,例如,我将使用垃圾值:
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";
现在,我将调用上面的排序命令(请注意,这些值并没有完全填充数组,有些容器中没有新数据。)
这就是代码,我想我已经把问题隔离出来了,排序部分工作不正常。如果你们还有任何问题,请在评论中发表。同样,如果你有一个解决这个令人费解的问题的可能办法,我很乐意听到
PS:我只是想说清楚,我让它工作了,然后我关闭了我的笔记本电脑,下一次启动(从那时起)它就不工作了 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
购买力平价:如果您想查看我目前使用的代码:
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
import java.util.*;
公开课考试
{
公共静态void main(字符串[]args)
{
字符串[][]fps=新字符串[2][15];
Arrays.fill(fps[0],“empty”);//填充所有空格,这样排序和搜索就不会出错
数组填充(fps[1],“0”);
//fps[ROW][column]=值+“”;
//fps[ROW][column]=值实例+“”;
fps[0][0]=“您没有fps!”;
fps[1][0]=0+“”;
fps[0][1]=“仅一个fps”;
fps[1][1]=1+“”;
fps[0][2]=“仅两个fps”;
fps[1][2]=2+“”;
fps[0][3]=“仅三个fps”;
fps[1][3]=3+“”;
fps[0][4]=“仅四个fps”;
fps[1][4]=4+“”;
fps[0][5]=“仅5 fps”;
fps[1][5]=5+“”;
fps[0][6]=“仅6 fps”;
fps[1][6]=6+“”;
fps[0][7]=“仅七帧”;
fps[1][7]=7+“”;
fps[0][8]=“仅8 fps”;
fps[1][8]=8+“”;
fps[0][9]=“仅9 fps”;
fps[1][9]=9+“”;
/*只能在这条线后用数组摸索*/
//要输入到方法中的内容:
//当前值(从行中)
//currentVariable(因此我们知道正在处理的数组的名称)
String currentValue=“Blah!”//这是将在数组中搜索的值,如果找到,则其子int将增加1,如果未找到,则将其添加到数组中。
//进行二进制排序,然后在事物的fps[0]端进行搜索,确保[1]与[0]更改一起携带。
sort(fps,新的Comparator(){
@凌驾
公共整数比较(最终字符串[]entry1,最终字符串[]entry2){
最终字符串time1=entry1[0];
最终字符串time2=entry2[0];
返回时间1.比较(时间2);
}
});
int searchIndex=Arrays.binarySearch(fps[0],currentValue);//获取当前搜索值的索引
System.out.println(searchIndex);//=0)//如果找到该值
{
fps[0][searchIndex]=(Integer.parseInt(fps[0][searchIndex])+1)+;//向该值添加一个实例
}//如果结束,则结束
else//else查找下一个打开的值点并将其更改为当前搜索查询(并将其实例分配给1
{
对于(int i=0;i
我相信您的索引是反向的。您正在排序的fps
fps
只有2个正在排序的元素。我相信您正在尝试对15个元素进行排序。如果您反向索引,我相信您将获得所需的排序
String[][] fps = new String[15][2];
在这种情况下,您可以考虑一个对象数组,而不是一个2D数组。它似乎是一种更为合理的结构,避免了这种混淆。
您是否有关于这些对象的任何资源(在与此类似的实例中使用时) 有关使用自定义对象时如何执行此操作的示例,请参见 其中一个维度有一个字符串,然后另一个维度有一个int(为方便起见存储为字符串)这并不方便,因为对数字的字符串表示形式进行排序与对数字进行排序不同。使用自定义对象将允许您正确存储数据,以便进行正确排序。除了中指出的问题外,这方面还存在一个问题:
int searchIndex = Arrays.binarySearch(fps[0], "Java");
<> P>使用自定义比较器排序,使用相同的自定义比较器进行二进制搜索。使用<代码> BialSaar(T[])、T键、比较器键,但是java、C++等示例的输出是什么?喜欢SeFieldRead。<代码>不为您提供汤!<代码>——这是当您在LAPPY上编码时发生的事情。“PS:我只是想说清楚,我让它工作了,然后关闭了我的lappy,下次启动时(从那时起)它就不工作了。”我知道我需要的是2个15元素的数组,你的设置是15个两元素的数组。
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
String[][] fps = new String[15][2];
int searchIndex = Arrays.binarySearch(fps[0], "Java");