Java 将两种不同数据类型的数组指定给对象数组
我还是一个编程新手,在编程方面有点麻烦 首先,我想创建一个程序,对字符串和int进行排序。正确的语法是什么?从我的搜索结果来看,merge和ArrayUtil.addAll是一种很好的方法,但对我来说不起作用 到目前为止我的代码是Java 将两种不同数据类型的数组指定给对象数组,java,arrays,Java,Arrays,我还是一个编程新手,在编程方面有点麻烦 首先,我想创建一个程序,对字符串和int进行排序。正确的语法是什么?从我的搜索结果来看,merge和ArrayUtil.addAll是一种很好的方法,但对我来说不起作用 到目前为止我的代码是 public static void main(String[] args) { int[] quizScore; int[] examScore; String[] names; int perfectQuizScore; int perfect
public static void main(String[] args) {
int[] quizScore;
int[] examScore;
String[] names;
int perfectQuizScore;
int perfectExamScore;
double[] quizGrade;
double[] examGrade;
double[] subjectGrade;
int size;
size = readInteger(0,100,"Enter number of students");
quizScore = new int[size];
examScore = new int[size];
names = new String[size];
quizGrade = new double[size];
examGrade = new double[size];
subjectGrade = new double[size];
perfectQuizScore = readInteger(0,1000,"Enter the perfect quiz score");
perfectExamScore = readInteger(0,1000,"Enter the perfect exam score");
for (int x=0; x<size; x++) {
names[x] = readString("Enter the name of student "+(x+1));
quizScore[x] = readInteger(0,perfectQuizScore,"Enter the quiz score of "+ names[x]);
examScore[x] = readInteger(0,perfectExamScore,"Enter the examination score of " + names[x]);
quizGrade[x] = computeGrade(quizScore[x],perfectQuizScore);
examGrade[x] = computeGrade(examScore[x], perfectExamScore);
subjectGrade[x] = (quizGrade[x] + examGrade[x])/ 2;
}
System.out.println("\n\nPerfect Quiz Score = " + perfectQuizScore);
System.out.println("\nPerfect Exam Score = " + perfectExamScore);
showData(names, quizScore, examScore, quizGrade, examGrade, subjectGrade);
double averageGrade = computeAverage(subjectGrade);
System.out.printf("%n%25s%5.2f%n", "Average Grade =", averageGrade);
double lowestGrade = findLowest(subjectGrade);
System.out.printf("%n%25s%5.2f%n", "Lowest Grade =", lowestGrade);
double highestGrade = findHighest(subjectGrade);
System.out.printf("%n%25s%5.2f%n", "Highest Grade =", highestGrade);
System.out.println();
System.out.println("Sorted Record based on grades");
parallelSortD(names, quizScore, examScore,quizGrade, examGrade, subjectGrade);
showData(names,quizScore,examScore,quizGrade,examGrade,subjectGrade);
System.out.println("Sorted Record based on names");
parallelSortD(names,quizScore,examScore,quizGrade,examGrade,subjectGrade);
showData(names,quizScore,examScore,quizGrade,examGrade,subjectGrade);
System.exit(0);
}
如果对字符串和整数对进行排序,并希望根据其整数伙伴对字符串进行排序,则可以使用
SortedMap
而不是数组:
SortedMap<Integer, String> map = new TreeMap<>();
map.put(10, "ten");
map.put(5, "five");
map.put(100, "hundred");
map.put(4, "four");
System.out.println(map.values());
SortedMap
将其条目按照键的顺序进行排序。现在看到了您的代码,我将创建一个学生类,其中包含name、quizScore、examcore、quizGrade、examGrade和subjectGrade字段。
将它们中的每一个添加到一个数组中(或者更好地说,添加到ArrayList
),然后使用比较器对其进行排序
可能会有帮助。首先,您需要确定您使用的编程语言,并将其设置为问题的标记。现在,您必须更清楚地了解目标以及您尝试的失败原因(提示:一个好的问题通常包含足够的代码以供复制)。
SortedMap<Integer, String> map = new TreeMap<>();
map.put(10, "ten");
map.put(5, "five");
map.put(100, "hundred");
map.put(4, "four");
System.out.println(map.values());
[four, five, ten, hundred]