Arraylist - 编译器让我困惑 [英] Arraylist - compiler is confusing me

查看:172
本文介绍了Arraylist - 编译器让我困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,当我编译这个简单的代码,一个错误弹出。 (如果我有10个rep我会post它)基本上说(文件目录)使用未检查或不安全的操作。使用-Xlint重新编译:取消选中详细信息。我试验了一点,看来如果我带走 Bin.add()的错误消失。有人可以解释我应该做什么吗?



  import java.util.ArrayList; 
public class Summoned_Bin
{

ArrayList Bin = new ArrayList();

Summoned_Bin()
{

}

void addToBin()
{
召唤召唤= new召唤();
int index = 0;
while(Bin.get(index)!= null)
{
index ++;
}
Bin.add(index,summoned); //没有它它运行良好
}

}


解决方案

我想它要你键入列表 List< Summon>需要注意的三件事:


$ b = p>将其类型声明为 List< Summon> 而不是 ArrayList< Summon>



Summoned_Bin 类应该遵循Java命名标准,因此 SummonedBin 应该是名称。



还有 SummonedBin object应遵循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);
}
}


For some reason, when I compile this simple code, an error pops up. (If I had 10 rep I would post it) It basically says (File Directory) uses unchecked or unsafe operations. Recompile with -Xlint: unchecked for details. I experimented a little and it seems if I take away the Bin.add() the error goes away. Can someone explain what I should do?

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
}

}

解决方案

I think it wants you to type the list List<Summon> Bin = new ArrayList<Summon>();

Three things to note:

Declare the type as List<Summon> instead of ArrayList<Summon> its best practice to use the interface, which will allow you to change the type at a later date.

The Summoned_Bin class should follow Java naming standards, so SummonedBin should be the name.

Also the name of the SummonedBin object should follow Java naming standards, use bin instead of Bin.

Revised Class

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); 
    }
}

这篇关于Arraylist - 编译器让我困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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