【问题标题】:Complex Data structure in Java multi-level collections (lists) within one class一个类中的Java多级集合(列表)中的复杂数据结构
【发布时间】:2016-03-27 15:56:07
【问题描述】:

我对 Java 比较陌生。我一直在努力定义一个满足我需求的类。在这个网站或谷歌上搜索可能没有,因为问题太具体了。任何帮助表示赞赏!

理想情况下,这个类(我们称之为 Filer)应该有:

  1. 名称(字符串)
  2. 卷(集合/字符串列表:0 -100) 反过来,每个卷将具有: A. 名称(字符串) B. Servers_Permed(另一个字符串集合/列表:0-40)

一旦我可以定义类,我就可以定义 getter 和 setter 来使用它,但到目前为止,我还没有完全迷失定义类 :-)

谢谢!

【问题讨论】:

    标签: java list data-structures collections


    【解决方案1】:

    好吧,一块一块地布置。

    首先,你有 Volume(不知道 Volume 怎么可能是“Collection/list of Strings: 0 -100”并且具有以下属性):

    public class Volume {
        String name;
        List<String> servers_permed;
    }
    

    现在你有了 Filer:

    public class Filer {
        String name;
        List<Volume> volumes;
    }
    

    您必须添加必要的构造函数,getter/setter。

    【讨论】:

      【解决方案2】:

      您的描述似乎不正确。

      你需要一个类 Filer:

      • 名称(字符串)
      • volumes (list of Volume)(不是你问的字符串,因为你对问题第二部分的解释不同,很明显你需要一个 Volume 列表而不是 String 列表)

      还有第二类卷:

      • 名称(字符串)
      • Servers_Permed(字符串列表)

      所以你需要一个像下面这样的数据结构:

      public class Volume {
          private String name;
          private List<String> serversPermed;  // Changed the name to a name more adherent to standard guidelines
      
          ...
      }
      
      public class Filer {
          private String name;
          private List<Volume> volumes;
      
          ...
      }
      

      【讨论】:

        【解决方案3】:

        上面给出的两个答案都很好用(反正它们几乎一样)。

        谢谢你,Davide 和 DBug!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-01
          • 1970-01-01
          • 2015-05-17
          • 1970-01-01
          • 1970-01-01
          • 2020-06-06
          • 1970-01-01
          相关资源
          最近更新 更多