Go through an ArrayList of Items

2

Good morning,

I can not go through an ArrayList that is made up of Items. Inside these Items I keep different information such as game name, launch date, URL, etc ...

With a foreach loop I can enter each Item, but I can not get specific information at this point, for example the name of the game. The ArrayList I created it in the following way:

ArrayList<Item> games = new ArrayList<Item>();

And I intend to go through it as follows:

for (Item game: games) {
    //código para acceder a cada campo del Item.
}

I have searched for information about the Items but I have not found anything, maybe it's called another way.

Inside the Item I keep information for each game, for example:

Name: Subnautica (Game Preview)
Type: ---
Descripcion Castellano: Este juego es un trabajo en curso...
Descripcion Pegi:     
Edad Pegi: 16+
Microsoft Store ID: bx3s1q5dvhrd 
Microsoft Store URL: subnautica-game-preview/bx3s1q5dvhrd    
Nota: 4.0    
Pertenece a: -
Tipología de juego: Juego, 
    
asked by JetLagFox 27.02.2017 в 12:35
source

4 answers

2

A very common way to traverse ArrayList is Iterator :

Iterator<Item> it = lista.iterator();
// mientras al iterador queda proximo juego
while(it.hasNext()){
    Item item=it.next();
    System.out.println(item.toString());
    System.out.println("tipo: " + item.tipo);
}

with a Item for example:

public class Item{
    public String nombre;
    public int type; // asumo codigo tres digitos?
    public String descCastellano;
    ...

    @Override
    public String toString(){
         return String.format("%s: %s", nombre, descCastellano);
    }
}
    
answered by 27.02.2017 в 14:11
1

The first thing would be to create the geters suitable for your item , for example for the name of the game in the Game class you should have a method like this.

public String getNombre(){
 return this.nombre
}

Understanding that in the game class you have an attribute name of type string. After that you just have to access the method from the loop.

for (Juego game: games) {
    System.out.println(game.getNombre());
}

On the other hand, you must insert objects in the ArrayList (it's obvious but since you did not put that part of the code I put it just in case) This is done with the method add() .

For this you create the object and then add it, so you can see how it works you can do this for example:

Juego j;

    for (i = 0; i<10; i++){
      j = new Juego();
      games.add(j);
    }

It will add 10 games to the list, you will have to add the name of the game, if you have it done so that it is done in the constructor it would be like this

Juego j;

    for (i = 0; i<10; i++){
      j = new Juego("Juego "+i);
      games.add(j);
    }

If you have it done to change the name with a setter it would be like this:

Juego j;

    for (i = 0; i<10; i++){
      j = new Juego();
      j.setNombre("Juego "+i);
      games.add(j);
    }

The setter would go in the game class and it would have this form:

public void setNombre (String nombre){
this.nombre = nombre;
}
    
answered by 27.02.2017 в 12:43
0

The ArrayList are created with the name of the class that stores what you want to collect. The most normal thing would be that your class that stores the games would be called Juego , and could be constructed in the following way:

public class Juego{
    String titulo;

    //Constructor
    public Juego(String titulo)
    {
        this.titulo = titulo;
    }

    public String getTitulo()
    {
        return titulo;
    }
}

Your ArrayList would have the following form:

ArrayList<Juego> juegos = new ArrayList<Juego>();

For a test you can do:

Juego juego = new Juego("lo que sea");
juegos.add(juego);

If your object that stores the games is called Juegos as I said before, you should call the get of the property you intend to collect, and you would collect it in a simple way as:

for (Juego game: juegos) {
    System.out.println(game.getTitulo());
}
    
answered by 27.02.2017 в 12:37
0

For the comments I've seen in other responses telling how you load your attributes I see that you load a Map included in your class Item :

item.getValues().put(Constants.PERTENECE, pertenece_a)

To access the loaded values you can request them at that Map for the key you used (each constant of Constants ). An example within your for would be:

game.getValues().get(Constants.PERTENECE)
    
answered by 28.02.2017 в 08:45