【问题标题】:method undefined for type Java, nested classes未定义 Java 类型的方法,嵌套类
【发布时间】:2012-10-08 17:49:30
【问题描述】:

我在一个学校项目的嵌套类中遇到了一些麻烦。 目前,我正在尝试编写一种将项目插入到参差不齐的数组数据结构中的方法。 它使用由嵌套类创建的对象来跟踪二维数组位置,以便获取要插入的索引。但是,我在该行收到错误“方法 findEnd(E) 未定义 RaggedArrayList.ListLoc 类型”:

insertLoc.findEnd(item)

我在 stackoverflow 和网络上都进行了广泛搜索,但尚未找到答案。如果我错过了它并且这是多余的(我知道有很多“未定义类型问题的方法”),那么我道歉。

这里是相关代码>>

ListLoc 对象的嵌套类:

private class ListLoc {
  public int level1Index;
  public int level2Index;

  public ListLoc() {}

  public ListLoc(int level1Index, int level2Index) {
     this.level1Index = level1Index;
     this.level2Index = level2Index;            
  }

  public int getLevel1Index() {
     return level1Index;
  }

  public int getLevel2Index() {
     return level2Index;
  }

  // since only used internally, can trust it is comparing 2 ListLoc's
  public boolean equals(Object otherObj) {
     return (this == otherObj);
  }
}

查找匹配项的最后一个索引的方法(不是 ListLoc 嵌套类的一部分):

private ListLoc findEnd(E item){
  E itemAtIndex;

  for (int i = topArray.length -1; i >= 0; i--) {
     L2Array nextArray = (L2Array)topArray[i];

     if (nextArray == null) {
        continue;

     } else {

        for (int j = nextArray.items.length -1; j >= 0; j--) {
           itemAtIndex = nextArray.items[j];
           if (itemAtIndex.equals(item)) {
              return new ListLoc(i, j+1);
           }
        }
     }
  }

  return null;

}

尝试向不规则数组添加新值的方法:

boolean add(E item){
  ListLoc insertLoc = new ListLoc();
  insertLoc.findEnd(item);

  int index1 = insertLoc.getLevel1Index();
  int index2 = insertLoc.getLevel2Index();

  L2Array insertArray = (L2Array)topArray[index1];
  insertArray.items[index2] = item;

  return true;

}

感谢您的任何意见。

【问题讨论】:

  • 不要再添加作业标签了。 :) 我们没有使用它。我已经为你删除了。
  • 一目了然,我会说这是因为您的方法是私有的,或者范围是错误的(该方法不在正确的位置)。你说该方法不在ListLoc 中,但你是按原样调用它。
  • 没有意识到作业标签不再被使用。对不起。感谢您的反馈

标签: java arrays nested-class


【解决方案1】:

我敢打赌,改变这一点:

ListLoc insertLoc = new ListLoc();
insertLoc.findEnd(item);

到这里:

ListLoc insertLoc = findEnd(item);

会解决你的问题。

您正在尝试在 ListLoc 类上调用 findEnd,但如果您实际查看 ListLoc,它并没有在那里定义。当您尝试在insertLoc 对象上调用findEnd 时,它会失败,因为insertLocListLoc 的一个实例,我们已经说过它不包含findEnd

话虽如此,我敢打赌findItem 实际上是在与add 方法相同的类中声明的(我们就叫它MyList),所以你想实际调用MyList.findEnd,不是不存在的ListLoc.findEnd

【讨论】:

  • 是的。做到了。显然,我还在学习。范围对我来说仍然是一个挑战。感谢您的帮助。
  • 在这种情况下考虑它的一种方法是对象上的. 访问器有助于定义范围。当您说insertLoc.findEnd 时,. 说,“在insertLocListLoc 类)的类中查找非静态方法和字段,并为insertLoc 对象调用或访问它们。”要访问 same 类中的方法和字段,请记住我们对findEnd 的调用与我们调用this.findEnd 完全相同(试试看!),因此它也定义了范围。但在这里,变量是this,它最终为我们提供了定义add 的类。希望有帮助:)
【解决方案2】:

您几乎回答了您自己的问题。您尝试在 ListLoc 对象上调用 findEnd,但 ListLoc 没有定义 findEnd 方法。你需要

a) 将 findLoc 的实现添加到 ListLoc 或

b) 在正确的对象上调用 findLoc(你没有提供关于其他类的信息,所以我不能说太多)

【讨论】:

    【解决方案3】:

    查找匹配项的最后一个索引的方法(不是 ListLoc 嵌套类的一部分):

    是的 - 它 不是 的一部分 ListLoc... 但是当你在这里调用它时:

    ListLoc insertLoc = new ListLoc();
    insertLoc.findEnd(item);
    

    ...您试图将其称为 类的一部分。不是,所以不能这样称呼它。

    要么把它移到ListLoc,要么改变你调用方法的方式。

    【讨论】:

      【解决方案4】:

      我不确定我是否阅读正确,但从您的解释看来 findEnd 方法是在您的类之外定义的,因此 ListLoc 确实没有该名称的方法...

      你的私有 ListLoc findEnd(E item) 是在哪里定义的?

      【讨论】:

        猜你喜欢
        • 2016-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-16
        • 2013-08-28
        • 2018-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多