I have 3 classes (WINDOW1, WINDOW2, LISTS) In the "WINDOW1" class, I will instantiate the class "LISTS" and fill an ArrayList from a method.
How can I go through that ArrayList from VENTANA2?
As I understand it, from Window 1 I create an object of the class lists, filling from that object the ArrayList.
And when I want to go from Window2 to the ArrayList, it does not allow it since they are 2 different objects.
Main class that instantiates the filling and the route of the array.
package menu;
public class Menu {
public static void main(String[] args) {
Menu1 m1 = new Menu1();
Menu2 m2 = new Menu2();
m2.Llenado();
m1.Recorrido();
}
}
In this class I have the creation of the Array and two methods which allow to fill the list and the other to go through it.
package menu;
import java.util.ArrayList;
public class Listas {
ArrayList<String> lista;
public Listas() {
lista = new ArrayList<>();
}
public String LlenarLista(String data) {
lista.add(data);
return data;
}
public void Recorrer() {
for (String res : lista) {
System.out.println(res);
}
}
}
Class that instantiates List and fills the Array with the method
package menu;
public class Menu2 {
Listas lista;
public Menu2() {
lista = new Listas();
}
public void Llenado(){
String info = "Hola mundo";
lista.LlenarLista(info);
}
}
Class that instantiates List and traverses the Array
package menu;
public class Menu1 {
Listas listas;
public Menu1() {
listas = new Listas();
}
public void Recorrido() {
listas.Recorrer();
}
}
I know this will not work because the two classes instantiate different objects.
"From the Menu2 I have to fill the Array and from Menu1 I have to go through it"