我正在尝试从3个数组中使用索引时获得一个值?

时间:2018-06-22 18:27:39

标签: java arrays indexing

name[5] = {Peter, James, Roger, Peter, Josę};

subject[5] = {English, Math, English, Science, Math};

grade[5] = {96, 67, 78, 84, 100};

我要在这里实现的目标是使主题获得个人的最高分。请注意名称Array中名称的重复,这是同一个人。我试图做的是使用一个映射来获得结果,但是对于3个数组,对于我目前拥有的Java级别来说,这是非常棘手的。我想获得学生的姓名,将其与他们的成绩相匹配,然后选择成绩最高的学科,因此基本上,每个学生的名字的回报将是一个值。像这样

英语 数学 英语 数学

3 个答案:

答案 0 :(得分:3)

我将创建一个名为getStudentsHighestMark的方法,该方法将使用名称和成绩数据。该方法将遍历成绩,并且仅由相关学生考虑那些成绩。您需要一个int来跟踪您看到的该名称的最高成绩,以及该成绩所对应课程的字符串。遍历成绩后,只需返回学生最高分数的课程名称即可。像这样:

public static void main(String[] args) {
    String[] names = {"Peter", "James", "Roger", "Peter", "Jose"};
    String[] subjects = {"English", "Math", "English", "Science", "Math"};
    int[] grades = {96, 67, 78, 84, 100};

    String petersBest = getStudentsHighestMark("Peter", names, subjects, grades);
    System.out.println("Peter's best is: " + petersBest); //Peter's best is: English
    String jamesBest = getStudentsHighestMark("James", names, subjects, grades);
    System.out.println("James's best is: " + jamesBest);  //James's best is: Math
    String rogersBest = getStudentsHighestMark("Roger", names, subjects, grades);
    System.out.println("Roger's best is: " + rogersBest);  //Roger's best is: English
    String josesBest = getStudentsHighestMark("Jose", names, subjects, grades);
    System.out.println("Jose's best is: " + josesBest);  //Jose's best is: Math
}

private static String getStudentsHighestMark(String name, String[] names, String[] subjects, int[] grades) {
    int highestGrade = 0;
    String bestCourse = "";
    for(int i = 0; i < names.length; i++) {
        if(names[i].equals(name) && grades[i] > highestGrade) {
            highestGrade = grades[i];
            bestCourse = subjects[i];
        }
    }
    return bestCourse;
}

答案 1 :(得分:1)

首先,我将获得与每个名称关联的索引列表:

HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
int index = 0;
for (String n: name) {
    if (!map.containsKey(n)) {
        map.put(n, new ArrayList<Integer>());
    }
    map.get(n).add(index);
    index++;
}

然后我将遍历每个名​​称:

for (String name : map.keySet()) {

获取他们的索引,并找到最高分的索引:

    List<Integer> indices = map.get(name);
    int maxScore = 0, maxIndex = 0;

    for (int index: indices) {
        if (grades[index] > maxScore) {
            maxIndex = index;
        }
    }

然后我将从主题数组中打印出相同的索引:

    System.out.println(name + " did best in " + subject[index]);
}

答案 2 :(得分:0)

Java 8解决方案:我将组成一个对象,并丢弃标记最低的对象。请在下面找到代码段。

public class Main {

public static void main(String[] args) {
    String[] names = {"Peter", "James", "Roger", "Peter", "Jose"};
    String[] subjects = {"English", "Math", "English", "Science", "Math"};
    int[] grades = {96, 67, 78, 84, 100};
    System.out.println(getStudentWithHighScoredSubjects(names, subjects, grades));
}

public static Map<String, String> getStudentWithHighScoredSubjects(String[] names, String[] subjects, int[] grades) {
    return IntStream.range(0, names.length)
            .mapToObj(i -> new StudentHighestGradeSubject(names[i], subjects[i], grades[i]))
            .collect(Collectors.toMap(StudentHighestGradeSubject::getName, Function.identity(), (s1, s2) -> {
                if (s1.getGrade() > s2.getGrade()) {
                    return s1;
                } else {
                    return s2;
                }
            }, LinkedHashMap::new))
            .entrySet()
            .stream()
            .collect(Collectors.toMap(Map.Entry::getKey, e-> e.getValue().getHighestGradeSubject(), (s1, s2) -> s2, LinkedHashMap::new));
}

private static class StudentHighestGradeSubject {
    private String name;
    private String highestGradeSubject;
    private int grade;

    public StudentHighestGradeSubject(String name, String highestGradeSubject, int grade) {
        this.name = name;
        this.highestGradeSubject = highestGradeSubject;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHighestGradeSubject() {
        return highestGradeSubject;
    }

    public void setHighestGradeSubject(String highestGradeSubject) {
        this.highestGradeSubject = highestGradeSubject;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudentHighestGradeSubject that = (StudentHighestGradeSubject) o;
        return Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}

}

上面给出了执行后的以下输出

{Peter=English, James=Math, Roger=English, Jose=Math}