我在关联两个数组的索引时遇到了一些问题,我似乎无法弄明白。例如,我有一个
ID数组{365,222,306,203,113,208,213}
和
得分数组{265,262,257,256,253,246,246}
他们完美匹配:ID [0]与得分[0]匹配,
任务:是将21对数字(ID号和分数)分别读入两个独立的数组。写出配对的数字,并按列标题从高到低排名。
到目前为止,我已经能够按降序成功打印分数,但无法将它们与正确的ID号成功关联。
感谢任何帮助。
到目前为止,这是我的代码:
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
public class Program408a
{
public static void main(String[]args)throws Exception
{
Scanner input = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
Scanner input1 = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
System.out.println("ID\tScore");
int count = 0;
while(input.hasNextInt())
{
count++;
input.nextLine();
}
int[] iD = new int [count];
int[] scores = new int[count];
for(int i = 0; i < count; i++ )
{
iD[i] = input1.nextInt();
scores[i] = input1.nextInt();
}
int[] iDCopy = new int[count];
int[] scoresCopy = new int[count];
System.arraycopy(iD, 0, iDCopy, 0, iD.length);
System.arraycopy(scores, 0, scoresCopy, 0, scores.length);
System.out.println(Arrays.toString(iD));
System.out.println(Arrays.toString(scores));
int max = scores[0];
int index = 0;
for(int i = 0; i < count; i++)
{
if(max <= scores[i])
{
max = scores[i];
index = i;
}
}
for (int i = 0; i < count; i++)
{
for (int x = i + 1; x < count; x++)
{
if (scoresCopy[i] < scoresCopy[x])
{
int holder = scoresCopy[i];
scoresCopy[i] = scoresCopy[x];
scoresCopy[x] = holder;
}
}
}
System.out.println(Arrays.toString(iDCopy));
System.out.println(Arrays.toString(scoresCopy));
}
}
答案 0 :(得分:2)
进行排序时,只需对iD
数组进行相同的更改:
...
int holder = scoresCopy[i];
int iDholder = iDCopy[i];
scoresCopy[i] = scoresCopy[x];
iDCopy[i] = iDCopy[x];
scoresCopy[x] = holder;
iDCopy[x] = iDholder;
...
顺便说一下,使用两个独立的数组是FORTRAN的做事方式。在现代编程语言中,您可以将两个数字组合成一个结构/记录/类,然后只使用一个数组作为元素。
答案 1 :(得分:0)
在交换 scoresCopy 的索引时,您应该交换 iDCopy 的索引。
在代码中进行此更改
for (int i = 0; i < count; i++)
{
for (int x = i + 1; x < count; x++)
{
if (scoresCopy[i] < scoresCopy[x])
{
int holder = scoresCopy[i];
scoresCopy[i] = scoresCopy[x];
scoresCopy[x] = holder;
holder = iDCopy[i];
iDCopy[i] = iDCopy[x];
iDCopy[x] = holder;
}
}
}
新代码应如下所示
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
public class Program408a
{
public static void main(String[]args)throws Exception
{
Scanner input = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
Scanner input1 = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
System.out.println("ID\tScore");
int count = 0;
while(input.hasNextInt())
{
count++;
input.nextLine();
}
int[] iD = new int [count];
int[] scores = new int[count];
for(int i = 0; i < count; i++ )
{
iD[i] = input1.nextInt();
scores[i] = input1.nextInt();
}
int[] iDCopy = new int[count];
int[] scoresCopy = new int[count];
System.arraycopy(iD, 0, iDCopy, 0, iD.length);
System.arraycopy(scores, 0, scoresCopy, 0, scores.length);
System.out.println(Arrays.toString(iD));
System.out.println(Arrays.toString(scores));
int max = scores[0];
int index = 0;
for(int i = 0; i < count; i++)
{
if(max <= scores[i])
{
max = scores[i];
index = i;
}
}
for (int i = 0; i < count; i++)
{
for (int x = i + 1; x < count; x++)
{
if (scoresCopy[i] < scoresCopy[x])
{
int holder = scoresCopy[i];
scoresCopy[i] = scoresCopy[x];
scoresCopy[x] = holder;
holder = iDCopy[i];
iDCopy[i] = iDCopy[x];
iDCopy[x] = holder;
}
}
}
System.out.println(Arrays.toString(iDCopy));
System.out.println(Arrays.toString(scoresCopy));
}
}
希望这可以解决您的问题。