public class CategoryObservationType extends CategoryObservation
{
private String code;
private String name;
private String categories;
private int catlimit = 1;
String[] Categories;
public CategoryObservationType(String name, String code)
{
this.name = name;
this.code = code;
Categories[catlimit] = new Categories(name,code);
catlimit ++;
}
public String getCode()
{
return code;
}
public String getName()
{
return name;
}
public String setName(String name)
{
this.name = name;
return name;
}
public String setCode(String code)
{
this.code = code;
return code;
}
}
我是新人,我不明白为什么我会收到无法找到符号类别的输出错误,它发生在第 24 行,categories[catlimit] = newcategories(name,code);
最佳答案
您已将Categories
定义为字符串数组:
String[] Categories;
如果这是您的意图,您需要将其初始化为字符串数组。但是,我发现您已执行以下操作:
Categories[/*some index*/] = new Categories(name,code);
对我来说,这表明您需要一个 Category
对象的数组,大概使用 catLimit
作为数组的大小(>注意:我是随心所欲地编码——这可能行不通!):
// Your field:
private Category[] categories = new Category[catLimit];
...
// In your method:
categories[/*some index*/] = new Category(name,code);
如果是这种情况,那么您还需要将 Category
定义为对象。这就是说,我看到你的类 CategoryObservationType
:我认为这是 Category
对象?如果是这样,我注意到的代码对此进行了更改(再次,从臀部编码):
// Your field:
private CategoryObservationType[] categories = new CategoryObservationType[catLimit];
...
// In your method:
categories[/*some index*/] = new CategoryObservationType(name,code);
您将在此处看到我已将方法中的 catLimit
替换为 /*some index*/
。如果 catLimit
是数组的大小(正如我怀疑的那样),那么使用它作为索引将为您提供访问数组外空间的异常。如果您来自其他编程语言,我知道这可能看起来违反直觉:其他语言允许您使用 myArray[sizeOfArray] = elementToAppend 语法来扩展数组。 在 Java 中无法做到这一点:您需要创建一个新数组,或者更简单的是,只需使用 LinkedList
作为集合类型。您将能够使用 get(index)
为其建立索引,并使用 add(index)
添加内容。
希望有帮助!
关于java - 找不到符号,尽管它是在 java 中定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36993395/