java - 在不同的方法(同一类)上访问 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());
}
}
}
}
解决方案
从您的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 方法中的参数将引用相同的列表。
我希望我能回答你的问题。。
推荐阅读
- excel - 如何访问嵌套字典的键?
- python - Apache Beam 数据流管道 (python) 中步骤的 If 语句
- javascript - 从 Firefox 开发者工具强制全屏
- docker - Kubectl 设置图像并以“rpc 错误:代码 = 4 desc = 超出上下文期限”结尾
- c# - 读取文件内存太大
- flutter - 如何防止页面被弹出?
- php - set_exception_handler() 期望参数 (exception_handler) 是一个有效的回调
- sql - 为什么不建议将产品规格存储在单列中?
- nativescript - Nativescript 6.1.1 应用程序在 --release 模式下崩溃,在调试中工作
- r - R非标准评估:将变量的名称作为参数传递给函数并让函数为其赋值