【问题标题】:Combine two arrays of different data type合并两个不同数据类型的数组
【发布时间】:2015-10-17 18:47:10
【问题描述】:
import java.util.Scanner;

public class scores
{
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args)
    {
        System.out.print("\f");
        int classSize, counterScore, counterName;
        String name;
        double score,average, sum;

        System.out.print("Enter size of class: ");
        classSize = input.nextInt();

        int[] scoreArray = new int[classSize];
        String[] nameArray = new String[classSize];

        counterScore=1;
        counterName = 1;
        average = 0;
        sum = 0;

        for (int x = 0; x < classSize; x++)
        {
            input.nextLine();
            System.out.print("Student " + counterName++ + " Name: ");
            nameArray[x] = input.nextLine();
            System.out.print("Student " + counterScore++ + " Score: ");
            scoreArray[x] = input.nextInt();

            sum = sum + scoreArray[x];
            average = sum / classSize;
        }

        System.out.println(average);
    }
}

我必须制作一个应用程序,让我知道有多少人参加了考试,然后输入他们的姓名和分数。我使用了两个不同的数组,一个是字符串,一个是双精度数组。我的输出旨在读取哪些名称低于平均值并显示名称。我不知道如何组合这两个数组,以便它识别出这个分数与这个名字有关,所以显示那个名字。

【问题讨论】:

  • 创建一个由名字和分数组成的类Person!?然后只创建一个 Person 数组
  • 你已经很清楚了。您不需要实际组合 2 个数组,只需意识到 2 个数组中的相同索引对应于同一个人。所以nameArray[4]和scoreArray[4]对应的是同一个人。
  • 或者使用Map&lt;String, Integer&gt;,其中键是名称,值是分数。但是,如果您以后想要添加第三个信息,这将不太可扩展。

标签: java arrays


【解决方案1】:

我认为你最好的选择是创建一个包含两个字段(名称和分数)的 POJO 并创建一个数组:

public class Student {
    private String name;
    private int score;

    public Student(String name, int score) {    
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }
}

【讨论】:

  • 我不知道这是不是有意的,但我喜欢这样一个事实,即实例是不可变的。而且由于“我们”都喜欢避免使用 equalshashCodetoString 实现这样的明显代码,因此可以考虑使用 Google 的 AutoValue 库来创建此类。
【解决方案2】:

您可以简单地在一次迭代中一起迭代这两个数组,并填充包含 String 和 double 的自定义类型的数组。 (即学生类)

public class Student {
    public String name;
    public double score;
    public Student(String name, double score) {
        this.name = name;
        this.score = score;
    }
}

List<Student> students = new ArrayList<Student>();

for (int x = 0; x < classSize; x++)
{
    input.nextLine();
    System.out.print("Student " + counterName++ + " Name: ");
    nameArray[x] = input.nextLine();
    System.out.print("Student " + counterScore++ + " Score: ");
    scoreArray[x] = input.nextInt();

    sum = sum + scoreArray[x];
    average = sum / classSize;

    // populate array of student
    students.add(new Student(nameArray[x], scoreArray[x]));
}

请注意,在这种情况下,您不再需要 scoreArraynameArray 以提高内存利用率。

【讨论】:

    【解决方案3】:

    你可以使用(在你的第一个循环之后添加这个):

    for (int i = 0; i < classSize; i++)
        {
            if(scoreArray[i] < average) {
                System.out.println(nameArray[i])
            }
        }
    

    或者,如果您想将所有内容集中在一行上:

    System.out.println("The following students are below average: ")
    boolean first = true;
    for (int i = 0; i < classSize; i++)
        {
            if(scoreArray[i] < average) {
                if(!first) {
                    System.out.println(", ");
                    first = false;
                }
                System.out.print(nameArray[i])
            }
        }
    

    另外,您应该将行 average = sum / classSize; 移到循环之外,每次都重新计算平均值是没有意义的。


    要找出最高值,为名称保留一个临时变量,为最高值保留另一个变量,然后循环遍历学生:

    String highestName = "";
    double highestValue = 0;
    for (int i = 0; i < classSize; i++) {
        if(scoreArray[i] > highestValue) {
            highestName = nameArray[i];
            highestValue = scoreArray[i];
        }
    }
    System.out.println(highestName + " has the highest grade.")
    

    如果打成平手,也可以使用它打印多个学生:

    String[] highestNames = new String[classSize];
    int numHighest = 0;
    double highestValue = 0;
    for (int i = 0; i < classSize; i++) {
        if(scoreArray[i] > highestValue) {
            highestNames[0] = nameArray[i];
            numHighest = 1;
            highestValue = scoreArray[i];
        } else if(scoreArray[i] > highestValue) {
            highestNames[numHighest] = nameArray[i];
            numHighest = numHighest + 1;
        }
    }
    
    System.out.println("The following student(s) has/have the highest grade: ")
    boolean first2 = true;
    for (int i = 0; i < numHighest; i++)
        {
            if(!first2) {
                System.out.println(", ");
                first2 = false;
            }
            System.out.print(highestNames[i])
            }
        }
    

    您还可以将用于打印成绩低于平均水平的学生的循环内容与用于查找最高成绩的循环内容结合起来,以提高您的程序效率:

    String[] highestNames = new String[classSize];
    int numHighest = 0;
    double highestValue = 0;
    System.out.println("The following students are below average: ")
    boolean first = true;
    for (int i = 0; i < classSize; i++)
        {
            if(scoreArray[i] < average) {
                if(!first) {
                    System.out.println(", ");
                    first = false;
                }
                System.out.print(nameArray[i])
            }
            if(scoreArray[i] > highestValue) {
                highestNames[0] = nameArray[i];
                numHighest = 1;
                highestValue = scoreArray[i];
            } else if(scoreArray[i] > highestValue) {
                highestNames[numHighest] = nameArray[i];
                numHighest = numHighest + 1;
            }
        }
    

    【讨论】:

    • 我能问一下你为什么把布尔值放进去吗?对不起,我是一个初学者,还在学习我能学到的东西。我发现这是最容易遵循的方法,但想知道如果你省略了这个和第二个 if 语句,它会起作用吗?
    • 如果你知道我怎么能找到最棒的最大值,我也必须列出获得最大值的名字
    • Andy465: boolean first 只是为了在列表中的第一个元素之前不添加逗号。
    猜你喜欢
    • 2018-04-08
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2014-10-01
    • 2015-11-28
    • 2022-01-01
    相关资源
    最近更新 更多