[英]Cannot Find Symbol Error when creating an object
I am creating an ArrayList object that contains a class.我正在创建一个包含 class 的 ArrayList object。 However, when I call a method from the WordList() class, it says "error: cannot find symbol obj.numWordsOfLength(2)"
但是,当我从 WordList() class 调用方法时,它显示“错误:找不到符号 obj.numWordsOfLength(2)”
import java.util.ArrayList;
public class WordList{
public static void main(String []args){
ArrayList obj = new ArrayList<WordList>();
obj.add("bird");
obj.add("wizard");
obj.add("to");
obj.numWordsOfLength(2);
}
private ArrayList<String> myList;
public WordList(){
myList = new ArrayList<String>();
}
public int numWordsOfLength(int len){
//Code
}
public void removeWordsOfLength(int len){
//Code
}
}
When you call obj.numWordsOfLength(2);
当你调用
obj.numWordsOfLength(2);
you're calling the method numWordsOfLength
from ArrayList (which does not exist), not from your WordList
class.您正在从 ArrayList(不存在)调用方法
numWordsOfLength
,而不是从您的WordList
class 调用方法。
First of all, you are adding String to ArrayList, not you're WordList object.首先,您将字符串添加到 ArrayList,而不是 WordList object。
I think what you're trying to achieve will look more like this:我认为您要实现的目标看起来更像这样:
public class WordList {
private List<String> wordList = new ArrayList<>();
public static void main(String[] args) {
WordList wordList = new WordList();
wordList.add("bird");
wordList.add("wizard");
wordList.add("to");
wordList.numWordsOfLength(2);
}
public void add(String word) {
wordList.add(word);
}
public int numWordsOfLength(int len) {
//Code
}
public void removeWordsOfLength(int len) {
//Code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.