[英]Error: The method add(java.lang.String) is undefined for the type java.lang.Object
Don't know why I'm getting this error. 不知道为什么我会收到此错误。 Working with ArrayLists and sorting words into appropriate ArrayLists by alphabetical order.
使用ArrayList并将单词按字母顺序排序到适当的ArrayList中。 If anyone can help me understand why I'm getting the error and how to fix it that would be great!
如果有人可以帮助我了解为什么我得到此错误以及如何修复它,那就太好了!
import java.util.*;
public class Sort {
public static ArrayList<Object> sortByFirstLetter( List<String> words) {
ArrayList<Object> bucket = new ArrayList<Object>();
for( int i = 0; i < 26; i++ ) {
ArrayList<String> letter = new ArrayList<String>();
bucket.add(letter);
}
for( String word : words ) {
int index = (int)(word.toLowerCase().charAt(0)) - 97; //get a number 0-25 for index; 97 is unicode for lwrcse "a"
System.out.println(index);
bucket.get(index).add(str); //THIS LINE GIVES ERROR
}
return bucket;
}
public static void main(String[] args) {
List<String> words = Arrays.asList("alex", "andy", "kevin");
sortByFirstLetter( words );
}
} }
It looks like bucket should be a List<List<String>>
, not a List<Object>
. 看起来bucket应该是
List<List<String>>
,而不是List<Object>
。 It's not clear why you made it a List of Object in the first place. 目前尚不清楚为什么首先要使它成为对象列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.