[英]ClassCastException: ArrayList cannot be cast to
This is the error that I am getting: 这是我得到的错误:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to MenuComponent
Here is the code that is causing me trouble: 这是引起我麻烦的代码:
import java.util.ArrayList;
import java.util.Iterator;
public class Menu extends MenuComponent{
ArrayList menuComponents = new ArrayList();
String name;
String description;
public Menu() {
}
public Menu(String name, String description) {
super();
this.name = name;
this.description = description;
}
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponents);
}
public void remove(MenuComponent menuComponent) {
menuComponents.remove(menuComponents);
}
public MenuComponent getChild(int i) {
return (MenuComponent) menuComponents.get(i);
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public void print() {
System.out.println(getName());
System.out.print("---" + getDescription());
System.out.println("---");
Iterator iterator = menuComponents.iterator();
while(iterator.hasNext()) {
MenuComponent menuComponent = (MenuComponent) iterator.next();
menuComponent.print();
}
}
public Iterator createIterator() {
return new CompositeIterator(menuComponents.iterator());
}
}
The problem that is causing the error is in this line: 导致错误的问题在此行中:
MenuComponent menuComponent = (MenuComponent) iterator.next();
I've tried messing with it, but am having no luck. 我试过弄乱它,但是没有运气。 Any tips are greatly appreciated. 任何提示,不胜感激。
That´s the disadvantage of using the raw type of the ArrayList
, you don´t notice if you do a simple mistake. 那是使用ArrayList
的原始类型的缺点,如果您犯了一个简单的错误,您将不会注意到。 You should rather declare the ArrayList
as ArrayList<MenuComponent> menuComponents
. 您应该将ArrayList
声明为ArrayList<MenuComponent> menuComponents
。 Then you would have noticed, that in your method add(MenuComponent menuComponent)
you are allways adding the arrayList to the arrayList, because you made a simple typo. 然后您会注意到,在方法add(MenuComponent menuComponent)
您总是将arrayList添加到arrayList中,因为您做了一个简单的错字。
It should be menuComponents.add(menuComponent);
它应该是menuComponents.add(menuComponent);
instead of menuComponents.add(menuComponents);
而不是menuComponents.add(menuComponents);
. 。 The same problem will occur in your remove method. 您的删除方法中也会发生相同的问题。
the problem is here: 问题在这里:
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponents);//you add menuComponentS to the list!
}
public void remove(MenuComponent menuComponent) {
menuComponents.remove(menuComponents);//you remove menuComponentS to the list!
}
The problem ist you add Arraylists in your Arraylist at this point 问题是此时您将Arraylist添加到Arraylist中
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponents);
}
it has to be 它一定要是
public void add(MenuComponent menuComponent) {
menuComponents.add(menuComponent);
}
you can simply avoid this by using typed Arraylist like this ArrayList<MenuComponent> menuComponents = new ArrayList<>();
您可以通过使用像这样的ArrayList<MenuComponent> menuComponents = new ArrayList<>();
来简单地避免这种情况ArrayList<MenuComponent> menuComponents = new ArrayList<>();
First, do not use raw collections, like this: 首先,不要使用原始集合,例如:
ArrayList menuComponents = new ArrayList();
The proper way, is like this: 正确的方法是这样的:
ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
Or better: 或更好:
List<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
This will allow you to eliminate the casts, like this: 这样就可以消除类型转换,如下所示:
return (MenuComponent) menuComponents.get(i);
and instead just do this: 而是这样做:
return menuComponents.get(i);
This will allow Java to tell you if you are sticking the wrong things in the array, as in here: 这将允许Java告诉您是否在数组中粘贴了错误的内容,如下所示:
menuComponents.add(menuComponents);
Java lets you get away with this because it has no idea what is supposed to be in menuComponents
. Java让您摆脱了menuComponents
因为它不知道menuComponents
应该包含什么。 When you say new ArrayList<MenuComponent>()
, you are telling Java that the ArrayList
can only have MenuComponent
objects in it. 当您说new ArrayList<MenuComponent>()
,您在告诉Java ArrayList
中只能包含MenuComponent
对象。 Then, the above code will fail to compile, because Java knows you are doing the wrong thing and can tell you. 然后,以上代码将无法编译,因为Java知道您做错了事并且可以告诉您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.