如何将一个数组输入与另一个数组输入相关联?

How do I relate one array input to another?

假设我有 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.");
}

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

那么你的名字就在这个索引上。

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

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

姓名[age.indexOf(最大)]

如果您有两个数组 name[]age[],您可以通过创建一些 class Person 并将这些数组中的条目类型的字段关联起来,并且获取人员列表 List<Person>,如下所示:

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?