【问题标题】:Trouble Defining Methods to interact with ArrayList麻烦定义与 ArrayList 交互的方法
【发布时间】:2019-01-24 09:14:03
【问题描述】:

我正在做一项学校作业,我需要使用我编写的一些方法从 ArrayList 访问数据。

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

    String fileName = "USPopulation.txt";
    File fileReader = new File(fileName);
    ArrayList<Integer> populations = new ArrayList<Integer>();
    Scanner inputFile = new Scanner(fileReader);

    while (inputFile.hasNext()) {
        populations.add(inputFile.nextInt());
    }

    System.out.println(populations.greatest());

    /**
 * Receives an ArrayList and returns the index of the value that has the
 * greatest increase from the previous value in the collection.
 * 
 * @param populations
 * @return greatestI
 */
    public static int greatest(ArrayList<Integer> populations) {

    int greatestDiff = 0;
    int greatestI = 0;
    int i = 0;
    while (i < populations.size() - 1) {
        int tempDiff = populations.get(i + 1) - populations.get(i);
        if (tempDiff >= greatestDiff) {
            greatestDiff = tempDiff;
            greatestI = i + 1;
        }
        i++;
    }
    return greatestI;
    }
}

当我尝试调用我的方法时,最棒的是,我遇到了错误

对于 ArrayList 类型,未定义方法 best()。

我以为我需要的定义包含在我的方法的参数中,但显然没有。

错误消息和我发现的故障排除使我似乎需要将我的 ArrayList populations 转换为方法知道如何处理的类型,但我尝试的任何方法似乎都不起作用。

感谢任何帮助。感谢任何花时间帮助菜鸟的人。 返回最小的I;

【问题讨论】:

  • 您似乎在努力学习 Java 语言基础知识。请重新访问任何体面的教程。这不是你调用方法的方式。

标签: java arraylist methods casting


【解决方案1】:

你应该这样称呼它

greatest(populations);

你调用它的方式就像调用一个在 Arraylist 类中定义的函数,它不是 .. 你的函数只是一个将 arraylist 作为输入的方法

【讨论】:

    【解决方案2】:

    这里有两件重要的事情你需要了解

    populations 是 ArrayList 类型的对象

    greatest() 是您在自己的类中编写的方法。它不是 ArrayList 类的一部分

    当您尝试访问populations.greatest() 时,您基本上是在尝试运行ArrayList 类中的方法greatest()。所以你应该得到一个异常"The method best() is undefined for the type ArrayList"

    您需要在您自己的类的对象上或以静态方式调用方法greatest(arrayListObject),而不是这样。由于您已经将您的方法定义为static,您可以通过传递对象populations 作为参数直接调用它,如下所示。

    System.out.println(greatest(populations));
    

    【讨论】:

      【解决方案3】:

      /*通过传递参数 population(arrayList) 调用您最强大的函数,并且您错过了主函数的右括号。
      */

      public static void main(String[] args) 抛出 IOException {

          String fileName = "USPopulation.txt";
          File fileReader = new File(fileName);
          ArrayList<Integer> populations = new ArrayList<Integer>();
          Scanner inputFile = new Scanner(fileReader);
      
          while (inputFile.hasNext()) {
              populations.add(inputFile.nextInt());
          }
      
          System.out.println(greatest(populations));
      }
      
      /**
       * Receives an ArrayList and returns the index of the value that has the
       * greatest increase from the previous value in the collection.
       * 
       * @param populations
       * @return greatestI
       */
      public static int greatest(ArrayList<Integer> populations) {
      
          int greatestDiff = 0;
          int greatestI = 0;
          int i = 0;
          while (i < populations.size() - 1) {
              int tempDiff = populations.get(i + 1) - populations.get(i);
              if (tempDiff >= greatestDiff) {
                  greatestDiff = tempDiff;
                  greatestI = i + 1;
              }
              i++;
          }
          return greatestI;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-16
        • 1970-01-01
        • 2012-03-29
        • 2013-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        相关资源
        最近更新 更多