Java中的结构化数组

时间:2016-05-19 07:39:47

标签: java objective-c arrays structured-array

我正在尝试将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"对象"?

5 个答案:

答案 0 :(得分:3)

如果你有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注释,而不是编写这个庞大的构造函数。它将在编译阶段为您生成。

答案 1 :(得分:2)

课程将如下所示。

function checkTaskStatus() {
    $.ajax({
        url: '/tasks/3dbf226a-a7f5-4e9b-8094-4ad1383aa6fd/status/',
        success: function(json) {
            if (json.task.status == 'SUCCESS') {
                // show a message to the user
            } else {
                setTimeout(checkTaskStatus, 10000); // try again in 10 seconds
            }
        }
    });
}

checkTaskStatus(); // check on load of the page

要使用它,请创建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; } }

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

希望这有帮助。

答案 2 :(得分:1)

试试这段代码:

this.isLeft Key

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

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

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

答案 3 :(得分:0)

您可以使用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, ...));
  }
}

答案 4 :(得分:0)

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

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