I've a class Persona with:
String name;
Int age;
And I have an ArrayList<Persona>
, then
I fill the ArrayList with a for loop which add(new Persona(name,age))
with a JSON parse data (this JSON parsing is from a PHP request to a MySQL DB which returns a random number of "Persona's").
When the loop finish, I want to get one of this Persona's, but Eclipse IDE said to me that to use get(i)
I have to declare the ArrayList like final
, and if I do that, I can't fill it.
This is the code:
ArrayList<Persona> personas = new ArrayList<Persona>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
personas = json.getJSONArray(TAG);
for(int i = 0; i < putas.length(); i++) {
JSONObject c = personas.getJSONObject(i);
String name = c.getString(TAG_NAME);
Int age = c.getString(TAG_AGE);
personas.add(new Persona(name, age));
} catch (...) {
...
}
int i = 4;
Persona p = personas.get(i);
With this code, IDE show me an error and "offers" me to add the "final" modifier to the ArrayList, but it's absurd.
PD: The problem is with an Android application, but I think that it's a Java problem.