【问题标题】:how do i match the index of two parallel arrays?如何匹配两个并行数组的索引?
【发布时间】:2012-10-08 22:31:58
【问题描述】:

我在一个文本文件中有一个在姓名和年龄之间交替的列表。有9个名字和9个年龄。

我需要显示我的程序

人(在此处插入姓名)具有最高年龄:(在此处插入年龄)。

到目前为止,这是我的代码:

     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

一切都编译我只是看不到使名称与年龄相匹配。帮忙?

【问题讨论】:

  • 我不认为这可以编译。 String name 的最高年龄()方法上缺少分号此外,由于您正在尝试并行数组,因此您应该使用 for(int i = 0; i < limit; i;++) 格式进行循环。这样你就可以保存找到值的实际索引
  • 这是我的问题 - 我如何保存实际索引?
  • 为什么highestAge() 需要学校和大小?你是指名字和年龄吗?
  • 我猜这是货物崇拜的功课。

标签: java arrays parallel-arrays


【解决方案1】:

如果你创建了一个 Person 类,你可以省去管理两个数组的麻烦。

这将是您的 Person 类:

class Person {
    String name;
    int age;
}

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

      【解决方案3】:

      离你不远了。您需要为最老年龄维护一个“索引”,然后您可以使用它在两个数组之间进行引用。

      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
      

      另外,我不知道你的示例是如何编译的,我没有看到数组 highestAge 在任何地方声明:P

      就我个人而言,我会创建一个 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;
          }
      }
      

      然后我会在循环的每次迭代中创建一个新的Person,并将它们添加到一个人数组中(Person[] people = new Person[...])(实际上,我会使用java.util.list 的实现,但这就是我)

      你会突然获得大量的创造力。

      您可以提供一个isOlderThen(Person person) 方法来比较人们。

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

      您实际上只是根据每个人的年龄对数组进行排序,使用 Arrays.sortComparator

      【讨论】:

        【解决方案4】:

        1) 创建一个类

        class person
        {
           String name;
           int    age;
        }
        

        2) 只创建一个数组:Person[] 个人

        3) 重写方法highestAge

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-08-31
          • 1970-01-01
          • 2020-12-01
          • 1970-01-01
          • 2016-08-02
          • 2018-02-22
          • 1970-01-01
          相关资源
          最近更新 更多