Create ArrayList, add several data to the same array from other classes and print them?

1

Finally, with the help of my teacher, I was able to achieve it, I attached the source code.

package prueba2;
import java.util.Scanner;
import java.util.ArrayList;

public class Concesionario {

public static void main(String[] args) {
    Lista list = new Lista();
    Socio socio = new Socio();
    Scanner scan = new Scanner(System.in);     
    int n = 0;
    System.out.println("Ingrese su Nombre");            
    String nombre = scan.nextLine();
    socio.setNombre(nombre);
    list.setSocios(socio.getNombre());
    String nombreList = list.getSocios();
    System.out.println(nombreList);
    list.array(nombre);
    }
}

public class Lista extends CompraVenta {
    static private Lista sc;
    private String socios;
    ArrayList<String> lista = new ArrayList();

public String getSocios() {
    return socios;
}

public void setSocios(String socios) {
    this.socios = socios;
}

public void array(String nombre) {
    lista.add(nombre);
    System.out.println(lista);
    }
}

public class CompraVenta {
    private Lista sc;

static public void Compra() {   
    Compra();                                           
    }
}
    
asked by Çhristofer Parra 22.06.2017 в 03:51
source

3 answers

1

To print the List you can simply use

System.out.println(sc.getSocios());

but this would surely print

[Christofer]

To print one or several elements contained in the List you can use a method:

private static void imprimeList(List<String> array){        
     for (String elemento: array) {
        System.out.println(elemento);
    }
}

This method you can add to your class and print the content of the elements of the list getSocios() that has visibility public

public class CompraVenta {

    private static void imprimeList(List<String> array){        
         for (String elemento: array) {
            System.out.println(elemento);
        }
    }

public void CompraVenta() {
    Lista sc = new Lista();
    sc.getSocios().add("Christofer");
    sc.getSocios().add("Elenasys");
    sc.getSocios().add("Ioana");
    sc.getSocios().add("Constantin");

    //System.out.println("sc");

    imprimeList(sc.getSocios());

   }
}

This would print as a result:

Christofer
Elenasys
Ioana
Constantin

Update :

Defines a class variable

private Lista sc;

Initialize it within your main () method:

sc = new Lista();

In your Purchase () method, just add the elements.

public void Compra() {
    Scanner scan = new Scanner(System.in);
 //   Lista sc = new Lista();
    System.out.println("Ingrese su Nombre");
    String nombre = scan.nextLine();
    sc.getSocios().add(nombre);
    imprimeList(soc.getSocios());
   }
}

The problem is that you are creating a new list instance every time you call the Buy () method, therefore it only contains one element at all times.

    
answered by 22.06.2017 / 16:56
source
1

Good morning,

ArrayList has its own implementation of toString (). You just have to change the line

 System.out.println("sc");

by:

System.out.println(sc.getSocios());

I've tried it and it already prints [Christofer].

    
answered by 22.06.2017 в 10:45
1

When printing a List either ArrayList or you have to indicate the position you want to show

 public static void main(String[] args) {
        ArrayList<String> lista = new ArrayList();
        lista.add("Paco");
        lista.add("Luis");
        lista.add("Felipe");

        for(int i=0;i<lista.size();i++){
            System.out.println(lista.get(i));
        }
    }

This would show

  

Paco

     

Luis

     

Felipe

This can also be done with Iterator and while but I think that with the for is simpler to see and if the list is of other objects other than String , it is normal that that object have a method toString() to be able to show the attributes of the object we would do.

System.out.println(lista.get(i).toString());
    
answered by 22.06.2017 в 17:08