将两种不同数据类型的数组分配到对象数组中

时间:2015-12-07 13:27:49

标签: java arrays

我还是比较新手编程,而且我对程序有点麻烦。

首先,我想创建一个程序,对一对String和int进行排序。它的正确语法是什么?从我搜索到的是合并和ArrayUtil.addAll是这样做的方式,但它对我不起作用。

到目前为止,我的代码就是这个......

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);
        }

现在我想要的是一个Object或一个包含这些数组的单个数组:

int[] quizScore;
int[] examScore;
String[] names;
double[] quizGrade;
double[] examGrade;
double[] subjectGrade;

2 个答案:

答案 0 :(得分:0)

如果您对字符串和整数进行了排序,并希望根据其Integer合作伙伴对字符串进行排序,则可以使用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());

结果是:

[four, five, ten, hundred]

SortedMap维护其条目按其键的顺序排序。

答案 1 :(得分:0)

现在看到你的代码,我会创建一个Student类,其中包含name,quizScore,examScore,quizGrade,examGrade和subjectGrade字段。 将其中的每一个添加到数组(或更好,ArrayList),然后使用Comparator对其进行排序。

This tutorial可能会有所帮助。