我已经用3个简单的类写了一些Java代码,其中第一个Controller有主方法并创建其他类的实例。 Floaters是一个创建Floater实例的链表的类,每个实例都有一个特定的长度和布尔值来表示它们是否垂直。我的问题,正如它在第一类的评论行中所说的那样,“人类”和“水獭”飞蚊症实例正在被分配相同的值并因此具有相同的大小......java链表问题
如何解决这个问题?
在此先感谢!
public class Controller{
private static Floaters humans;
private static Floaters otters;
public static void main(String[] args)
{
otters = new Floaters();
humans = new Floaters();
otters.addFloater(2, true);
otters.addFloater(3, true);
//this should read "2" and it does
System.out.println(otters.size());
//this should read "0" but reads "2". Why?
//How can I get it to read "0"?
System.out.println(humans.size());
}
}
import java.util.LinkedList;
public class Floaters {
private static LinkedList<Floater> llf;
Floaters()
{
llf = new LinkedList<Floater>();
}
public void addFloater(int length, boolean is_vertical)
{
Floater floater = new Floater(is_vertical, (byte)length);
llf.add(floater);
}
public int size()
{
return llf.size();
}
}
public class Floater {
int length;
boolean is_vertical;
Floater(boolean is_vertical, int length)
{
this.length = length;
this.is_vertical = is_vertical;
}
}