Manipulate ArrayList from different classes

0

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"

    
asked by Jorge Martini Schwenke 22.05.2018 в 08:44
source

2 answers

1

Because you do not create an instance of Lista and pass it as an argument to the instances of the classes Menu . This way in all the objects Menu you will be working with the same reference of the list.

public static void main(String[] args) {

    Lista lista = new Lista();

    Menu1 m1 = new Menu1(lista);
    Menu2 m2 = new Menu2(lista);

    // ...

}

Remember to receive the object from the list in the constructor of Menu objects.

Menu1

public class Menu1 {

    Listas lista;

    public Menu1(Lista lista) {
        this.lista = lista;
    }

    // ...
}

Menu2

public class Menu2 {

    Listas lista;

    public Menu2(Lista lista) {
        this.lista = lista;
    }

    // ...
}
    
answered by 22.05.2018 в 23:30
-1

If you want Menu1 to be able to access the Menu2 list, why not add a method that provides it?

public class Menu2 {
    Listas listas;

public Listas dameLista() { return listas;}
/etc
}

Then in Menu1 you can add a method that displays the list:

public class Menu1 {

    public void recorrido(Listas lista) { listas.recorrer();}
///etc
}

And in your Menu class:

public class Menu {

    public static void main(String[] args) {

        Menu1 m1 = new Menu1();
        Menu2 m2 = new Menu2();

        m2.Llenado();
        m1.Recorrido(m2.dameLista());

    }
 }
    
answered by 22.05.2018 в 17:21