由于某种原因,当我编译这段简单的代码时,出现错误。 (如果我有 10 个代表,我会发布它)它基本上说(文件目录)使用未经检查或不安全的操作。使用 -Xlint 重新编译:未选中详细信息。我做了一些实验,似乎如果我拿走 Bin.add()
错误就会消失。有人可以解释我应该做什么吗?
import java.util.ArrayList;
public class Summoned_Bin
{
ArrayList Bin = new ArrayList();
Summoned_Bin()
{
}
void addToBin()
{
Summon summoned = new Summon();
int index = 0;
while (Bin.get(index) != null)
{
index++;
}
Bin.add(index , summoned ); //Without this it runs fine
}
}
最佳答案
我认为它希望您键入列表 List<Summon> Bin = new ArrayList<Summon>();
注意三点:
将类型声明为 List<Summon>
而不是 ArrayList<Summon>
最佳做法是使用界面,这将允许您在以后更改类型。
Summoned_Bin
类应遵循 Java 命名标准,因此 SummonedBin
应该是名字。
也是 SummonedBin
的名称对象应遵循 Java 命名标准,使用 bin
而不是 Bin
.
修改类
public class SummonedBin {
List<Summon> bin = new ArrayList<Summon>();
SummonedBin() {
}
void addToBin() {
Summon summoned = new Summon();
int index = 0;
while (bin.get(index) != null) {
index++;
}
bin.add(index, summoned);
}
}
关于java - Arraylist - 编译器让我感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20557723/