将对象添加到arraylist会产生标识错误? [英] Adding an object to a arraylist creates an identifier error?

查看:108
本文介绍了将对象添加到arraylist会产生标识错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的一个小组项目中有一些很长的代码。我们都将自己的单独类添加到军队类中,该类包含显示每个不同类的GUI(如Archer,Ninja,Catapult等)。



无论如何,我想创建一个记录军队实例化的每个对象的ArrayList。但是,一旦我尝试向ArrayList添加任何东西,它就会给我错误< identifier> ;预期



我添加它们时这两行编译正常:

I have some big long code from a group project I'm working on. We all add our own separate classes to an "Army" class that consists of a GUI showing each of our different classes (like "Archer, Ninja, Catapult, etc.).

Anyway, I want to create an ArrayList that records each of the objects instantiated by the Army. However, as soon as I try to add anything to the ArrayList, it gives me the error <identifier> expected.

These two lines compile fine when I add them:

   ArrayList<object>armyList = new ArrayList<object>();
   Warrior warrior = new Warrior();
But as soon as I add the line <pre lang="java"> armyList.add(warrior);

它给了我上面提到的错误。



发生什么事了? />


这是完整的鳕鱼陆军的一员(我不想把这个问题空间搞得一团糟)。



我试过的:



广泛研究互联网。确保我正确地声明并添加到ArrayLists。将问题隔离到一个单独的文件中。

It gives me the aforementioned error.

What's going on??

Here's the full code of the Army class (I didn't want to clutter up this question space with the full thing).

What I have tried:

Researching the Internet extensively. Making sure I am declaring and adding to ArrayLists correctly. Isolating the problem into a separate file.

推荐答案

结果我无法直接在类中调用方法(如.add()方法),而不是一个方法。问题解决了:/
Turns out I couldn't call methods (like the .add() method) from directly within the class, not in a method. Problem solved :/


这篇关于将对象添加到arraylist会产生标识错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆