java ClassCastException: ArrayList 不能转换为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33036053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:06:45  来源:igfitidea点击:

ClassCastException: ArrayList cannot be cast to

javaarraylistiterator

提问by S. Peterson

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.

我试过弄乱它,但我没有运气。非常感谢任何提示。

回答by SomeJavaGuy

That′s the disadvantage of using the raw type of the ArrayList, you don′t notice if you do a simple mistake. You should rather declare the ArrayListas 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.

这就是使用原始类型 的缺点,ArrayList如果您犯了一个简单的错误,您就不会注意到。你应该声明ArrayListas ArrayList<MenuComponent> menuComponents。然后你会注意到,在你的方法中add(MenuComponent menuComponent)你总是将 arrayList 添加到 arrayList,因为你犯了一个简单的错字。

It should be menuComponents.add(menuComponent);instead of menuComponents.add(menuComponents);. The same problem will occur in your remove method.

它应该menuComponents.add(menuComponent);代替menuComponents.add(menuComponents);. 在您的 remove 方法中也会出现同样的问题。

回答by JohnnyAW

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!
}

回答by ScreamingTree

The problem ist you add Arraylists in your Arraylist at this point

问题是您此时在 Arraylist 中添加 Arraylists

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 来简单地避免这种情况 ArrayList<MenuComponent> menuComponents = new ArrayList<>();

回答by sh0rug0ru

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 supposedto be in menuComponents. When you say new ArrayList<MenuComponent>(), you are telling Java that the ArrayListcan only have MenuComponentobjects in it. Then, the above code will fail to compile, because Java knows you are doing the wrong thing and can tell you.

Java允许你得逞,是因为它不知道什么是应该是在menuComponents。当您说 时new ArrayList<MenuComponent>(),您是在告诉 Java 中ArrayList只能有MenuComponent对象。那么,上面的代码就会编译不通过,因为Java知道你做错了并且可以告诉你。