首页 > 解决方案 > 在不同的方法(同一类)上访问 ArrayList

问题描述

我试图在“verJugadores”方法中访问 ArrayList“Lista”(在“Guardar”方法中进行了修改),这可能吗?当我打印“seePlayer”方法的 ArrayList 时,我注意到它是空的,但是,“save”方法上的那个里面有一个元素(属性在另一个类上分配了一个值),这里是代码:

public class Jugador extends VentanaJugador{
private int puntuacion;
String edad1, edad;
String nombre;
String nom;
ArrayList<Jugador>lista=new ArrayList<>();
Jugador hola;

    public Jugador() {
        super();
    }
    

    public Jugador(int puntuacion, String nombre, String edad1, ArrayList lista) {
        super();
        this.hola = new Jugador();
        this.puntuacion = puntuacion;
        this.nombre = nombre;
        this.edad1 = edad1;
        this.lista = lista;
    }

    public int getPuntuacion() {
        return puntuacion;
    }

    public void setPuntuacion(int puntuacion) {
        this.puntuacion = 0;
    }

    public String getNombre() {
        return nombre;
    }

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

    public String getEdad() {
        return edad1;
    }

    public void setEdad(String edad1) {
        this.edad1 = edad1;
    }
    
    
     @Override
public String toString() {
    return "Nombre: " + this.getNombre() + "\n" +
           "Edad: " + this.getEdad();
}

    /**
     *
     * @param nom
     * @param edad
     * @return
     */
    public ArrayList guardar(String nom, String edad){
    nombre = nom;
    edad1=edad;
    this.hola = new Jugador(puntuacion, nombre, edad1, lista);
    lista.add(hola);
        System.out.println("*******************");
        System.out.println("Nombre: "+hola.getNombre()+"\n"+
                "Edad: "+hola.getEdad()+"\n"+
                        "Puntuacion Inicial: "+hola.getPuntuacion());
        System.out.println("*******************");
    return lista;
        
        
    
}
   
    public void verJugadores(){
        System.out.println("*******************");
        System.out.println("La lista de jugadores es:"+
                "\n"+
                "*******************");
        if(lista.isEmpty()){
            System.out.println("La lista esta vacia");
        }else{
    for (int i=0;i<lista.size();i++) {
        System.out.println("Nombre: "+lista.get(i).getNombre());
        System.out.println("Edad: "+lista.get(i).getEdad());
        System.out.println("Puntuacion: "+lista.get(i).getPuntuacion());
    }
    }
    }
    
   

}

标签: javaarraylistmethods

解决方案


从您的guardar 方法调用verJugadores 方法,同时发送您的ArrayList,在本例中为lista,作为参数。

public ArrayList guardar(String nom, String edad){
    nombre = nom;
    edad1=edad;
    this.hola = new Jugador(puntuacion, nombre, edad1, lista);
    lista.add(hola);
        System.out.println("*******************");
        System.out.println("Nombre: "+hola.getNombre()+"\n"+
                "Edad: "+hola.getEdad()+"\n"+
                        "Puntuacion Inicial: "+hola.getPuntuacion());
        System.out.println("*******************");

    
    verJugadores(lista); // calling the verJugadores method. 
    return lista;

然后,相应地在您的 verJugadores 中进行更改。

public void verJugadores(ArrayList<Jugador> lista){ // here parameter added.. 
        System.out.println("*******************");
        System.out.println("La lista de jugadores es:"+
                "\n"+
                "*******************");
        if(lista.isEmpty()){
            System.out.println("La lista esta vacia");
        }else{
    for (int i=0;i<lista.size();i++) {
        System.out.println("Nombre: "+lista.get(i).getNombre());
        System.out.println("Edad: "+lista.get(i).getEdad());
        System.out.println("Puntuacion: "+lista.get(i).getPuntuacion());
    }

    

由于 arraylist 是一种非原始数据类型,verJugadores 方法中的参数将引用相同的列表。

我希望我能回答你的问题。。


推荐阅读