简体   繁体   English

ClassCastException:无法将ArrayList强制转换为

[英]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.

相关问题 ClassCastException:ArrayList不能转换为String - ClassCastException: ArrayList cannot be cast to String 无法将 ClassCastException 对象强制转换为 - ClassCastException Object cannot be cast to java.lang.ClassCastException: java.util.Arrays$ArrayList 不能转换为 java.util.ArrayList - java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList java.lang.ClassCastException:java.util.ArrayList无法强制转换为java.lang.String - java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String java.lang.ClassCastException:无法将java.util.ArrayList强制转换为com.inrev.crm.bean.IRSurveyQuestions - java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.inrev.crm.bean.IRSurveyQuestions java.lang.ClassCastException: java.util.Arrays$ArrayList 不能在 DAO 中转换为 java.lang.Integer - java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.lang.Integer in DAO Retrofit2:ClassCastException:java.util.ArrayList无法强制转换为retrofit2.Call - Retrofit2: ClassCastException: java.util.ArrayList cannot be cast to retrofit2.Call 处理 java.lang.ClassCastException: java.util.ArrayList 不能强制转换为 checksAccount.BankEntry - Dealing java.lang.ClassCastException: java.util.ArrayList cannot be cast to checkingAccount.BankEntry 如何修复:java.lang.ClassCastException:java.util.ArrayList无法强制转换为double [] - How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to double[] java.lang.ClassCastException:com.mongodb.client.internal.AggregateIterableImpl 无法转换为 java.util.ArrayList - java.lang.ClassCastException: com.mongodb.client.internal.AggregateIterableImpl cannot be cast to java.util.ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM