3

I am trying to migrate my Objective C code into Java to learn this programming language.

I would like to "convert" the following ObjectiveC structure into Java, but I couldn't find the equivalent Java structure:

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

Which is the similar Java "object"?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142

5 Answers5

3

It is easy if you have YourClass class and the constructor with all instance fields. Just create an array of YourClass.

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

The class will look like

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

The names of the fields are not pretty, I've used OP's names for his understanding.


With Project Lombok you could write @AllArgsConstructor annotation above the class and not write this massive constructor. It will be generated at the compile stage for you.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
2

The class will look like this.

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

Where you want to use it, create an ArrayList<MaxFemales> mFemaleList = new ArrayList<MaxFemales>();

Construct the class, giving all arguments and simple add it to the array list.

Hope this helps.

vibhor_shri
  • 374
  • 2
  • 12
1

Try this code:

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

}

Then, in your activity class MainActivity.java you can declare a structure for males and another for females

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

Last, you can use of the getter/setter methods in this way:

descriptorMale[1].getBeginner();
descriptorFemale[3].setFromAge(18);
eldivino87
  • 1,425
  • 1
  • 17
  • 30
0

You can implement this in Java with an ArrayList.

Example code:

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, ...));
  }
}
ZeusNet
  • 710
  • 9
  • 25
0

Dictionaries are called maps in Java. Instead of copy and pasting code like this I would suggest reading up a bit on different types of Java maps. They are not conceptually difficult to understand if you understand dictionaries and function nearly identically.

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

mathlete
  • 185
  • 13