简体   繁体   English

我如何匹配两个并行数组的索引?

[英]how do i match the index of two parallel arrays?

i have a list in a text file that alternates between name and age. 我在文本文件中有一个在名称和年龄之间交替的列表。 there are 9 names and 9 ages. 有9个名字和9个年龄。

i need my program to display 我需要我的程序才能显示

Person (insert name here) has the highest age: (insert age here). 人(在此插入姓名)的年龄最高:(在此插入年龄)。

here is my code so far: 到目前为止,这是我的代码:

     public class Names 
     {


public static void main(String[] args) throws Exception
{

      // get textfile
      Scanner input = new Scanner( new File("names_and_ages.txt") );


final String[] names= new String[9];
final int[] ages= new int[9];
int counter = 0;

while (input.hasNext()) //while not end-of-file
{
    names[counter]=input.next();  
    ages[counter]=Integer.parseInt(input.next());
    counter++;

}
highestAge(names, ages);
input.close();
}//end of main

public static void highestAge (String[] name, int[] age)
{
    String name;
int count = 0;
int oldest = highestAge[count];
for ( int number: highestAge)
{
    if (number > oldest)
        {
        oldest = number;
        }
}
System.out.print("Person " + name + " has the highest age: " + oldest );
}//end of Size method

}//end of class

everything compiles i just can't seen to make the name match the age. 一切都编译了,我只是看不到使名称与年龄匹配。 help? 救命?

If you created a Person class, you could save yourself the trouble of managing two arrays. 如果创建了Person类,则可以省去管理两个数组的麻烦。

This would be your Person class: 这将是您的Person类:

class Person {
    String name;
    int age;
}
public static void highestAge(String[] names, int[] ages)
{
    //SET SENSIBLE DEFAULTS TO oldest AND index
    int index = 0;
    int oldest = ages[0];
    //Looping based on index
    for (int i = 0;  i < ages.length; i++)
    {
        if (ages[i] > oldest)
        {
           index = i;
           oldest = ages[i];
        }
}
System.out.print("Person " + names[index] + " has the highest age: " + ages[index] );
}//end of Size method

You're not far off. 你不远。 You need to maintain an "index" to the oldest age, which you can then use to reference between the two arrays. 您需要维护最旧的年龄的“索引”,然后可以使用它在两个数组之间进行引用。

public static void highestAge (String[] names, int[] ages)
{
    String name
    int oldest = 0;
    int oldestIndex = -1;
    for (int index = 0; index < ages.length; index++) 
    {
        if (ages[index] > oldest)
        {
            oldest = number;
            oldestIndex = index;
        }
    }
    System.out.print("Person " + names[oldestIndex] + " has the highest age: " + oldest );
}//end of Size method

Also, I don't know how your example compiles, I don't see the array highestAge declared any where :P 另外,我不知道您的示例如何编译,我看不到highestAge数组highestAge任何地方声明了:P

Personally, I'd create a Person class that has the name and age as fields... 就个人而言,我将创建一个名称和年龄作为字段的Person类。

public class Person {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

I would then create a new Person on each iteration of the loop and add them to an array of people ( Person[] people = new Person[...] ) (actually, I'd use an implementation of a java.util.list but that's me) 然后,我将在循环的每次迭代中创建一个新的Person并将它们添加到人员数组中( Person[] people = new Person[...] )(实际上,我将使用java.util.list的实现java.util.list但就是我)

You suddenly get a whole lot of creativity opening up for you. 突然间,您将获得大量创造力。

You could provide a isOlderThen(Person person) method to compare people together. 您可以提供isOlderThen(Person person)方法来比较人们。

if (person.isOlderThen(oldest)) {
    oldest = person;
}

You actually just sort the array based on the age of each person, using Arrays.sort and a Comparator 您实际上只是使用Arrays.sortComparator根据每个人的年龄对数组进行Arrays.sort

1) create a class 1)创建一个类

class person
{
   String name;
   int    age;
}

2) create only one array: Person[] persons 2)仅创建一个数组:Person []人

3) Rewrite the method highestAge 3)重写方法highestAge

Person getOldest( Person[] persons )
{
   Person oldest = null;
   for( Person person : persons )
   {
      if( oldest == null || person.age > oldest.age )
      {
         oldest = person;
      }
   }
   return oldest;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM