繁体   English   中英

Java中的结构化数组

[英]Structured array in Java

我正在尝试将Objective C代码迁移到Java中以学习这种编程语言。

我想将以下ObjectiveC结构“转换”为Java,但我找不到等效的Java结构:

g_vo2MaxFemales = @[
                      @{
                          @"fromAge": @(13),
                          @"tillAge": @(19),
                          @"beginner": @(29.95),
                          @"regular": @(36.95),
                          @"pro": @(40.5)
                      },
                      @{
                          @"fromAge": @(20),
                          @"tillAge": @(29),
                          @"beginner": @(28.25),
                          @"regular": @(35),
                          @"pro": @(39)
                      }
                      ...
                ];

哪个是类似的Java“对象”?

如果你有YourClass类和带有所有实例字段的构造函数,这很容易。 只需创建一个YourClass数组。

YourClass[] array = {
    new YourClass(13, 19, 29.95, 36.95, 40.5),
    new YourClass(...),
    ...
};

课程看起来像

class YourClass {
    private int fromAge;
    private int tillAge;
    private double beginner;
    private double regular;
    private double pro;

    public YourClass(int fromAge, int tillAge, double beginner, double regular, double pro) {
         this.fromAge = fromAge;
         this.tillAge = tillAge;
         this.beginner = beginner;
         this.regular = regular;
         this.pro = pro;
    }
 }

这些字段的名称并不漂亮,我使用了OP的名字来理解他。


使用Project Lombok,您可以在类上面编写@AllArgsConstructor注释,而不是编写这个庞大的构造函数。 它将在编译阶段为您生成。

这个课程看起来像这样。

public class MaxFemales {

private int fromAge;
private int tillAge;
private double beginner;
private double regular;
private double pro;

public MaxFemales(int fromAge, int tillAge, double beginner, double regular, double pro) {
    this.fromAge = fromAge;
    this.tillAge = tillAge;
    this.beginner = beginner;
    this.regular = regular;
    this.pro = pro;
}

public int getFromAge() {
    return fromAge;
}

public void setFromAge(int fromAge) {
    this.fromAge = fromAge;
}

public int getTillAge() {
    return tillAge;
}

public void setTillAge(int tillAge) {
    this.tillAge = tillAge;
}

public double getBeginner() {
    return beginner;
}

public void setBeginner(double beginner) {
    this.beginner = beginner;
}

public double getRegular() {
    return regular;
}

public void setRegular(double regular) {
    this.regular = regular;
}

public double getPro() {
    return pro;
}

public void setPro(double pro) {
    this.pro = pro;
}
}

如果要使用它,请创建一个ArrayList<MaxFemales> mFemaleList = new ArrayList<MaxFemales>();

构造类,给出所有参数并简单地将它添加到数组列表中。

希望这可以帮助。

试试这段代码:

Descriptor.java

public class Descriptor {

    private int fromAge;
    private int tillAge;
    private float beginner;
    private float regular;
    private float pro;

    // CONSTRUCTORS

    public Descriptor() {}

    public Descriptor(int fromAge, int tillAge, float beginner, float regular, float pro) {
        this.fromAge = fromAge;
        this.tillAge = tillAge;
        this.beginner = beginner;
        this.regular = regular;
        this.pro = pro;
    }

    // SETTER

    public void setTillAge(int tillAge) {
        this.tillAge = tillAge;
    }

    public void setFromAge(int fromAge) {
        this.fromAge = fromAge;
    }

    public void setBeginner(float beginner) {
        this.beginner = beginner;
    }

    public void setRegular(float regular) {
        this.regular = regular;
    }

    public void setPro(float pro) {
        this.pro = pro;
    }

    // GETTER

    public int getTillAge() {
        return tillAge;
    }

    public int getFromAge() {
        return fromAge;
    }

    public float getBeginner() {
        return beginner;
    }

    public float getRegular() {
        return regular;
    }

    public float getPro() {
        return pro;
    }

}

然后,在您的活动类MainActivity.java您可以为男性声明一个结构,为女性声明另一个结构

    Descriptor[] descriptorMale = {
            new Descriptor(13,  19, 5f, 7f, 9f),
            new Descriptor(20,  29, 6f, 13f, 16f),
            new Descriptor(30,  39, 7f, 18f, 19f),
            new Descriptor(40,  49, 8f, 29f, 33f),
            new Descriptor(50,  59, 9f, 30f, 37f),
            new Descriptor(60, 120, 10f, 41f, 44f)
    };

    Descriptor[] descriptorFemale = {
            new Descriptor(13,  19, 5f, 7f, 9f),
            new Descriptor(20,  29, 6f, 13f, 16f),
            new Descriptor(30,  39, 7f, 18f, 19f),
            new Descriptor(40,  49, 8f, 29f, 33f),
            new Descriptor(50,  59, 9f, 30f, 37f),
            new Descriptor(60, 120, 10f, 41f, 44f)
    };

最后,您可以通过以下方式使用getter / setter方法:

descriptorMale[1].getBeginner();
descriptorFemale[3].setFromAge(18);

您可以使用ArrayList在Java中实现此功能。

示例代码:

class ModelObject {
  private Integer fromAge;
  private Integer tillAge;

  // followed by the other attributes

  public ModelObject(Integer fromAge, Integer tillAge, ...){
    this.fromAge = fromAge;
    this.tillAge = tillAge;
  }

  public Integer getFromAge(){
    return this.fromAge;
  }

  public Integer getTillAge(){
    return this.tillAge;
  }

  //getter for the outher attributes

  public void setFromAge(Integer fromAge){
    this.fromAge = fromAge;
  }

  public void setTillAge(Integer tillAge){
    this.tillAge = tillAge;
  }

  //setter for the outher attributes

  public static void main(String[] args) {
      List<ModelObject> list = new ArrayList<ModelObject>();
      list.add(new ModelObject(fromAge, tillAge, ...));
  }
}

字典在Java中称为映射。 而不是像这样复制和粘贴代码,我建议在不同类型的Java地图上阅读一些内容。 如果您理解字典和功能几乎相同,则在概念上难以理解它们。

http://www.tutorialspoint.com/java/java_map_interface.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM