java链表问题

发布于 2024-10-01 07:32:26 字数 1407 浏览 2 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧街凉风 2024-10-08 07:32:26

Floaters 类中的 llf静态。当您将变量设为静态时,它们会链接到类而不是实例,因此 Floaters 的两个实例都使用相同的列表。

要纠正此问题,只需从变量声明中删除 static 即可。

The llf in your Floaters-class is static. When you make variables static, they're linked to the class rather than the instance, and thus both instances of Floaters use the same list.

To correct this, simply remove the static from your declaration of the variable.

太傻旳人生 2024-10-08 07:32:26

在漂浮物中,llf 不应该是静态的

in floaters, llf should NOT be static

风流物 2024-10-08 07:32:26

因为static

private static LinkedList<Floater> llf;

在这种情况下,static 表示类字段,在类的所有实例之间共享。

Because of static:

private static LinkedList<Floater> llf;

In this case static means a class field, shared among all instances of a class.

人心善变 2024-10-08 07:32:26

例如,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文