java链表问题
我用 3 个简单的类编写了一些 Java 代码,其中第一个类(Controller)具有 main 方法并创建其他类的实例。 Floaters 是一个创建 Floater 实例链接列表的类,每个实例都有特定的长度和布尔值来表示它们是否垂直。我的问题,正如第一堂课的注释行中所说,是“人类”和“水獭”Floaters 实例都被分配了相同的值,因此具有相同的大小......
有关如何解决此问题的任何建议?
提前致谢!
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;
}
}
I have written some Java code with 3 simple classes where the first, Controller, has the main method and creates the instances of the other classes. Floaters is a classes that creates a linked list of Floater instances, each with a particular length and boolean value to say if they are vertical or not. My problem, as it says in the commented lines of the first class, is that both "humans" and "otters" Floaters instances are getting assigned the same values and thus have the same size....
Any suggestions on how to fix this?
Thanks in advance!
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Floaters
类中的llf
是静态。当您将变量设为静态时,它们会链接到类而不是实例,因此Floaters
的两个实例都使用相同的列表。要纠正此问题,只需从变量声明中删除
static
即可。The
llf
in yourFloaters
-class is static. When you make variables static, they're linked to the class rather than the instance, and thus both instances ofFloaters
use the same list.To correct this, simply remove the
static
from your declaration of the variable.在漂浮物中,llf 不应该是静态的
in floaters, llf should NOT be static
因为
static
:在这种情况下,
static
表示类字段,在类的所有实例之间共享。Because of
static
:In this case
static
means a class field, shared among all instances of a class.例如,Java 中的数学函数被声明为 java.lang.Math 类的静态方法,数学常量是该类的静态属性。因此,如果您使用 sin(x),则始终使用相同的方法。
For example - mathematic functions in Java are declared as static metohods of the class java.lang.Math, matemathematical constants are static atributes of this class. So if you use sin(x), you are using always the same method.