1

I have a model: fase.java with Integers and Strings + getters and setters:

public class Fase implements Serializable {
private Integer age;
private String name;
}

I want to store both the Integer and String in a Array or ArrayList. I now use this:

public String[] getAllValues(){
String[] values = {age.toString(), name};
return values;

Then in dataServiceImpl.java I retrieve the data with:

user.getFase().getAllValues()[0]; 

and retrieve the age.

This works, but I have a lot more than age and name, and was thinking if I could put everything in Fase.java in one Array/ArrayList, because they are Integer and String, and then retrieve it in dataServiceImpl.java?

Something like this in Fase.java: ArrayList <Objects> f3Values = new ArrayList <Objects>();

or Fase [] f3Array = new Fase[34];

and then retrieve that in dataServiceImpl.java with: ArrayList<Fase3.Fase3Array> f3List = new ArrayList<Fase3.Fase3Array>(); and use something like: user.f3List[0]; ?

MOTIVECODEX
  • 2,624
  • 14
  • 43
  • 78
  • 3
    just store the Fase objects in the array/`ArrayList`... `ArrayList f3List = new ArrayList();` ... what's the problem with that? –  Jan 19 '15 at 14:52
  • because it returns nothing. While there is data and with public String[] getAllValues(), it returns the data. – MOTIVECODEX Jan 19 '15 at 15:13

2 Answers2

5

First, you should learn how Java works.

Is Java "pass-by-reference" or "pass-by-value"?

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Then, you should learn how to properly create an encapsulated class, by defining both constructor(s) and getters, methods, setters (if needed; note that setters in general break encapsulation) etc.

Then, you should understand that to aggregate data you:

  1. create a class, i.e. definition object that holds all the necessary fields,

  2. create a storage aggregate (array, ArrayList, Map, whatever),

3a. create an object of a given class, setting the values of the fields,

3b. add the object to the aggregate,

3c. goto 3a until the aggregate is filled with the data needed.

Explaining that on the code provided, you should first have

public class Fase implements Serializable {
  private int age;
  private String name;

  public Fase( int age, String name ) {
    this.age = age;
    this.name = name;
  }

  public int getAge() { return age; }
  public String getName() { return name; }
}

then you can create the aggregate, e.g.

int FASE_MAX = 34;
Fase[] fArray = new Fase[FASE_MAX];
ArrayList<Fase> fArrayList = new ArrayList<Fase>(FASE_MAX);

then you create the objects and add them to the aggregate, e.g.

for( int i = 0; i < FASE_MAX; i++ ) {
  Fase newFase = new Fase( i, "John Doe" );
  fArrayList.add( newFase );
  fArray[i] = newFase;
}

then, and only then, you can access the aggregate:

Fase someFase = fArrayList.get( n );
Fase someOtherFase = fArray[n];
  • 1
    @F4LLCON this post is 100% correct. You should follow everything vaxquis posted here. The most important points: 1) the `Fase` class encapsulates each name/age pair. This approach is easier to manage than storing the name/age pair directly to some list or array. 2) Understand that the `Fase` object contains the data, while the `ArrayList` and/or Array contains **REFERENCES** (not copy of the data) to each `Fase` object (basically a pointer to the location where the data is stored). – hfontanez Jan 19 '15 at 15:37
-1

Your Fase class can have whatever members and however many members you like and you can access them all. If you want an array of Fase then create one and each element of the array will contain all the Fase members.

Fase[] myArray = new Fase[34];

You have an array of 34 "Fase's" just add whatever members you want to your Fase class.

mgrenier
  • 1,409
  • 3
  • 21
  • 45
  • If you want an arraylist it works the same, just create an arraylist of "Fase's" rather than an array. You have done all the work here, I don't know where your problem is. Just add the additional members you want to your Fase class and voila, you are done. – mgrenier Jan 19 '15 at 15:02
  • The problem is that it returns nothing. While there is data and with public String[] getAllValues(), it returns the data. – MOTIVECODEX Jan 19 '15 at 15:14
  • -1 : `Fase[] myArray = new Fase[34];` **doesn't** create an array of 34 `Fase` objects; it merely creates an array of 34 `Fase` object **references**, with no data nor objects there whatsoever, all equal to `null`. the same's with `ArrayList` - trying to get an object from `new ArrayList(34)` with no elements added will result in `NoSuchElementException` –  Jan 19 '15 at 15:25
  • I didn't realize he was asking how to populate an array or array list, in his question he never asked "how to I populate an array" he said he has more values other than "name" and "age" to store there. So I said, you can just add them to your class. – mgrenier Jan 19 '15 at 15:28
  • 1
    While I agree the question was vague and imprecise, the exact statement from your answer is, quote: `Fase[] myArray = new Fase[34];` You have an array of 34 "Fase's" - and that's obviously false *per se*. –  Jan 19 '15 at 15:31