Fill List in Java

1

I'm starting to work with List in Java, my problem is this, I'm trying to fill in a list of objects, the classes are the following: Class Changes:

public class Cambios {

    private List<Cambio> cambios;

    public List<Cambio> getCambios() {
        return cambios;
    }

    public void setCambios(List<Cambio> cambios) {
        this.cambios = cambios;
    }
}

Change Class:

public class Cambio {
    String idCard;
    String idList;


    public String getIdCard() {
        return idCard;
    }
    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }
    public String getIdList() {
        return idList;
    }
    public void setIdList(String idList) {
        this.idList = idList;
    }
}

I am trying to fill them in the following way through the set, but I do not know very well how the lists work, thanks in advance

Cambios cambios;
Cambio cambio;
List <Cambio> cambioLista = null;

for (int i=0; i<10; i++){
   String idCard= "Valor de id" + i);
    cambioLista.get(i).setIdCard(idCard);
   cambios.setCambios(cambioLista);
}
    
asked by Silvia 12.04.2018 в 19:18
source

2 answers

1

To create a list of objects you need to initialize the list first. In your case the list is a property of the class Cambios , so it is necessary to create an object of that class, instantiate it and then initialize the list:

Cambios cambios = new Cambios();
cambios.setCambios(new ArrayList<>()); // inicializamos lista

To fill the list we call the method add and pass it an object of the type from which the list is declared. In your case, the list will contain objects of class Cambio , so we need to create objects of that class first and then add them to the list:

for (int i = 0; i < 10; i++) {
   Cambio cambio = new Cambio();
   cambio.setIdCard("El idCard es: " + i); // asignamos idCard
   cambio.setIdList("El idList es: " + i); // asignamos idList
   cambios.getCambios().add(cambio); // agregamos objeto a la lista
}
    
answered by 12.04.2018 / 21:14
source
1

What you are using is an interface, you can use it as a reference variable or implement it directly in the class and overwrite its methods but you can never have an instance of any object of that type.

I recommend using the class ArrayList , it will allow you to create a list of objects of the type defined within of modifying ArrayList <E> . Here is your modified code.

First class

import java.util.ArrayList;

public class Cambios {

    private ArrayList<Cambio> cambios;

    public ArrayList<Cambio> getCambios() {
        return cambios;
    }

    public void setCambios(ArrayList<Cambio> cambios) {
        this.cambios = cambios;
    }
}

Second class

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

        ArrayList<Cambios> listacambios = new ArrayList<Cambios>(); // Crea un objeto ArrayList

        for(int i=0;i<5;i++) {

            listacambios.add(new Cambio()); // Crea un objeto de tipo Cambio y almacena una referencia dentro del ArrayList

        }

        Cambios cambios = new Cambios(); // Crea un objeto de tipo Cambios
        cambios.setCambios(listacambios); // Pasa como argumento del método una referencia al ArrayList

        ArrayList<Cambio> listacambios = cambios.getCambios(); // Crea una nueva referencia de tipo ArrayList y asignale el objeto ArrayList dentro de la clase cambios.

        String idCard;        

        for(int i=0;i<listacambios.size();i++) {

            idCard = "Valor de id: "+i;

            listacambios.get(i).setIdCard(idCard);

        }
    }
}

There are ways to optimize the code to not be a redudante, but for reasons of understanding not to be drastic with the modifications I did it in this way, I recommend studying the extended for and review the API of the ArrayList class to learn more about his methods.

    
answered by 12.04.2018 в 20:02