【问题标题】:How do I relate one array input to another?如何将一个数组输入与另一个相关联?
【发布时间】:2021-05-15 14:22:17
【问题描述】:

假设我有 2 个扫描仪填充数组,name[]age[]。每个都按顺序填写。如果我要找到数组中最年长的人,如何使用数组打印出他们的姓名和年龄? 例如age[] 中最大的条目是78。有没有办法将它与name[] 数组关联起来打印出来?

参考代码:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many entries ?");

    int entries;
    do {
        entries = input.nextInt();
        if (entries < 1) {
            System.out.println("please enter a valid number!");
        }
    } while (entries < 1);

    String[] name = new String[entries];
    String[] gender = new String[entries];
    int[] age = new int[entries];

    for (int i = 0; i < entries; i++) {
        System.out.println("Enter the name of person No" + (i + 1) + ".");
        name[i] = input.next();
    }

    double ageSum = 0;
    int max = 0;
    for (int i = 0; i < entries; i++) {
        System.out.println("How old is " + name[i] + " ?");
        age[i] = input.nextInt();
        ageSum += age[i];
        max = Math.max(max, age[i]);
    }

    System.out.println("the oldest person is "
            + name[] + " whose " + max + " years old.");
}

【问题讨论】:

    标签: java arrays sorting java.util.scanner


    【解决方案1】:

    假设您的数组具有相同的大小和与名称对应的年龄,那么您可以检查最高年龄并存储具有最高年龄的元素的索引。

    那么你的名字就在这个指数上。

    int highestAgeIndice = 3; //indice of element with age 97 as example
    
    names[highestAgeIndice] // the corresponding name
    

    计算最高年龄并存储其指数

    int max = 0;
    int highestInd = 0;
    for (int i = 0; i < age.length; i++) {
        if (age[i] > max) {
            max = age[i];
            highestInd = i;
        }
    }
    
    System.out.println("the oldest person is " +
            name[highestInd] + " whose " + max + " years old.");
    

    代码

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("How many entries ?");
    
        int entries;
        do {
            entries = input.nextInt();
            if (entries < 1) {
                System.out.println("please enter a valid number!");
            }
        } while (entries < 1);
    
        String[] name = new String[entries];
        String[] gender = new String[entries];
        int[] age = new int[entries];
    
        for (int i = 0; i < entries; i++) {
            System.out.println("Enter the name of person No" + (i + 1) + ".");
            name[i] = input.next();
        }
    
        double ageSum = 0;
        for (int i = 0; i < entries; i++) {
            System.out.println("How old is " + name[i] + " ?");
            age[i] = input.nextInt();
            ageSum += age[i];
        }
    
        int max = 0;
        int highestInd = 0;
        for (int i = 0; i < age.length; i++) {
            if (age[i] > max) {
                max = age[i];
                highestInd = i;
            }
        }
        System.out.println("the oldest person is " +
                name[highestInd] + " whose " + max + " years old.");
    }
    

    【讨论】:

      【解决方案2】:

      如果你有两个数组name[]age[],你可以通过创建一些类Person 来关联它们,这些类的字段类型为这些数组中的条目,并获取人员列表List&lt;Person&gt;,类似这样:

      static class Person {
          String name;
          int age;
      
          public Person(String name, int age) {
              this.name = name;
              this.age = age;
          }
      
          public String getName() { return name; }
          public int getAge() { return age; }
      
          @Override
          public String toString() {
              return "Person{name='" + name + "', age=" + age + '}';
          }
      }
      
      public static void main(String[] args) {
          String[] name = {"Junior", "Senior", "Middle"};
          int[] age = {25, 78, 40};
      
          List<Person> people = IntStream.range(0, name.length)
                  .mapToObj(i -> new Person(name[i], age[i]))
                  .collect(Collectors.toList());
      
          // sort by age in reverse order
          people.sort(Comparator.comparing(
                  Person::getAge, Comparator.reverseOrder()));
      
          // output
          people.forEach(System.out::println);
      }
      

      输出:

      Person{name='Senior', age=78}
      Person{name='Middle', age=40}
      Person{name='Junior', age=25}
      

      另见:How do I sort two arrays in relation to each other?

      【讨论】:

        【解决方案3】:

        你可以在你的数组年龄上使用 indexOf 作为最大年龄,它会告诉你相关名称的索引。

        名字[age.indexOf(Max)]

        【讨论】:

        • 我收到一条错误消息:“无法在数组类型 int[] 上调用 indexOf(int)”
        • 你说得对,我很抱歉。我只是在回答一个关于字符串数组的问题,该函数适用于 Java 中的字符串或 ArrayLists。
        • 您可以在 for 循环之前声明一个变量索引,当您设置 max 时,将索引变量设置为循环中的当前 I。这将告诉您从哪个索引设置最大值。如果有帮助,别忘了投票!:D
        猜你喜欢
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        • 2013-01-14
        • 2019-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多