Java 中的结构化数组

Structured array in Java

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

我想 "convert" 将以下 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"object"?

如果您有 YourClass class 和包含所有实例字段的构造函数,这很容易。只需创建一个 YourClass.

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

class 看起来像

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,您可以在 class 上方编写 @AllArgsConstructor 注释,而不是编写这个庞大的构造函数。它将在编译阶段为您生成。

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

示例代码:

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, ...));
  }
}

class 看起来像这样。

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>();

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

希望对您有所帮助。

词典在Java中被称为地图。我建议不要像这样复制和粘贴代码,而是阅读不同类型的 Java 地图。如果您理解词典并且功能几乎相同,那么它们在概念上并不难理解。

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

试试这个代码:

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;
    }

}

然后,在您的 activity class 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);