Help with arraylist, Modify elements?

2

Good I do not understand how I modify the data already assigned, I know I would have to use the set methods, but I can not do it.

 String array[][]= new String [list.size()][6];

        for (int i = 0; i < list.size(); i++) {
            array[i][0] = list.get(i).getId();
            array[i][1] = list.get(i).getNombre();
            array[i][2] = list.get(i).getApellido();
            array[i][3] = list.get(i).getEdad();
            array[i][4] = list.get(i).getDireccion();
            array[i][5] = list.get(i).getTelefono();
        }

    
asked by Danserver 19.05.2017 в 23:37
source

3 answers

1

You must use the Setters. I have made a complete example well commented so you can understand how they are used and everything you can do with them.

Patient Class:

public class Paciente {

    // Variables
    private int Id;
    private String nombre;
    private String apellido;
    private int edad;
    private String direccion;
    private int telefono;

    // Constructor
    public Paciente(int id, String nombre, String apellido, int edad, String direccion, int telefono) {
        super();
        Id = id;
        this.nombre = nombre;
        this.apellido = apellido;
        this.edad = edad;
        this.direccion = direccion;
        this.telefono = telefono;
    }

    // Getters
    public int getId() {
        return Id;
    }

    public String getNombre() {
        return nombre;
    }

    public String getApellido() {
        return apellido;
    }

    public int getEdad() {
        return edad;
    }

    public String getDireccion() {
        return direccion;
    }

    public int getTelefono() {
        return telefono;
    }

    // Setters
    public void setId(int id) {
        Id = id;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public void setApellido(String apellido) {
        this.apellido = apellido;
    }

    public void setEdad(int edad) {
        this.edad = edad;
    }

    public void setDireccion(String direccion) {
        this.direccion = direccion;
    }

    public void setTelefono(int telefono) {
        this.telefono = telefono;
    }

}

Example Class (with the "main" method):

import java.util.ArrayList;
import java.util.List;

public class Ejemplo {

    public static void main(String[] args) {

        // Crear pacientes
        Paciente paciente1 = new Paciente(1, "Ejemplo1", "Ejemplo", 21, "Ejemplo", 5558888);
        Paciente paciente2 = new Paciente(2, "Ejemplo2", "Ejemplo", 22, "Ejemplo", 5558888);
        Paciente paciente3 = new Paciente(3, "Ejemplo3", "Ejemplo", 23, "Ejemplo", 5558888);
        Paciente paciente4 = new Paciente(4, "Ejemplo4", "Ejemplo", 24, "Ejemplo", 5558888);
        Paciente paciente5 = new Paciente(5, "Ejemplo5", "Ejemplo", 25, "Ejemplo", 5558888);
        Paciente paciente6 = new Paciente(6, "Ejemplo6", "Ejemplo", 26, "Ejemplo", 5558888);

        // Agregar pacientes a una lista
        List<Paciente> pacientes = new ArrayList<Paciente>();
        pacientes.add(paciente1);
        pacientes.add(paciente2);
        pacientes.add(paciente3);
        pacientes.add(paciente4);
        pacientes.add(paciente5);
        pacientes.add(paciente6);

        // Imprimir datos de los pacientes (usando un loop For-each introducido en "Java 8")
        System.out.println("Todos los pacientes:");
        for (Paciente pacienteActual : pacientes) {
            System.out.println(pacienteActual.getId() + ", " + 
                    pacienteActual.getNombre() + ", " + 
                    pacienteActual.getApellido() + ", " + 
                    pacienteActual.getEdad() + ", " + 
                    pacienteActual.getDireccion() + ", " + 
                    pacienteActual.getTelefono());
        }

        // Obtener los datos del paciente con el ID = 4
        System.out.println("\nPaciente con el ID = 4:");
        System.out.println(pacientes.get(3).getId() + ", " +
                pacientes.get(3).getNombre() + ", " +
                pacientes.get(3).getApellido() + ", " + 
                pacientes.get(3).getEdad() + ", " + 
                pacientes.get(3).getDireccion() + ", " + 
                pacientes.get(3).getTelefono());

        // Modificar el nombre del paciente con el ID = 2 e imprimirlo
        System.out.println("\nNombre nuevo del paciente con el ID = 2:");
        pacientes.get(1).setNombre("Nuevo nombre");
        System.out.println(pacientes.get(1).getId() + ", " + pacientes.get(1).getNombre());

        // Modificando los datos de todos los pacientes
        for (Paciente pacienteActual : pacientes) {
            pacienteActual.setId(11);
            pacienteActual.setNombre("Otro1");
            pacienteActual.setApellido("Otro");
            pacienteActual.setEdad(51);
            pacienteActual.setDireccion("Nueva");
            pacienteActual.setTelefono(123456);
        }

        // Vaciar la lista de pacientes
        pacientes.clear();
        System.out.println("\nTamaño de la lista: " + pacientes.size());
    }
}

Result:

I hope I have helped you, regards!

    
answered by 10.05.2018 в 23:33
0

You have a list that contains several objects, you can access the object defined in the list using the index and modify its properties using the setter methods.

For example, if you want the 3 item in the list (index 2, since it starts at 0), and you want to modify the Name and Surname properties, for example, you would do it this way:

list.get(2).setNombre("Dan");
list.get(2).getApellido("Server");

in this way you would modify the properties of that object in the list.

    
answered by 19.05.2017 в 23:59
0

You have to use the setters:

array[i][0] = list.get(i).setId(2);
array[i][1] = list.get(i).setNombre("John Doe");

Lists are composed of objects. If the class of the objects that your list is composed of does not have the setters you need, you will not be able to call them.

Summary: Implement the setters in the Person class or as the class that uses your list is called.

    
answered by 02.02.2018 в 19:41