Help with ArrayList in Java

2

Schedule a list using the ArrayList class The list must contain at least 8 attributes.

So far I have the Class already created with its attributes, empty constructor, constructors, getter and setter and finally toString. My class is called Carreras. It's a java class

There is even everything right but I do not understand how to make the list: this is a java application

  • public static void CarrerasListado () {
  • ArrayList List = new ArrayList li>
  • List.add (Which is supposed to be added in parentheses)
  • asked by Sword 13.11.2018 в 19:37
    source

    3 answers

    1

    Save what you set for the Careers class in an ArrayList ()

    Carreras carreras = new Carreras();
    
    ArrayList<Carreras> arrayCarreras = new ArrayList<>();
    
    carreras.setNombreCarrera("Carrera 1");
    carreras.setFechaCarrera("13-11-2018");
    
    arrayCarreras.add(carreras);
    

    This way we keep what is in your career within the array

    Get the elements of the race

     for(int i = 0 ; i < arrayCarreras.size() ; i++){
    
        System.out.println("Dato nombre carrera: "+arrayCarreras.get(i).getNombreCarrera());
    
     }
    

    Or you can also scroll through the list using the new Java 8 (streams) utilities. For example:

    arrayCarreras.forEach(c -> System.out.println("Dato nombre carrera: "+c.getNombreCarrera()));
    
        
    answered by 13.11.2018 / 21:11
    source
    1

    Schedule a list that has 8 attributes, the truth is that I do not understand what it means.

    I sense that what you are asking is that you make a list of an object that contains at least 8 attributes.

    For that you need to create a class with at least 8 attributes, in your case I run with its constructor initializing those attributes and their getters and setters as you well say.

    Later, when you have finished your class, in the main you have to create the objects: Career c = new Career (attributesClass) These attributes of the class you have to write yourself, are the data that will appeal the attributes of the race. I explain: If your career has an attribute called name for example (suppose the race has a name) you must put the name of the race "Indianapolis" (for example). Numeric values do not have quotes.

    After having created as many objects as you want and having declared your list ArrayListlista = new ArrayList () You can do the long-awaited list.add (c) where c is the name of the variable that holds one of the races you've created.

        
    answered by 13.11.2018 в 20:30
    1

    You must specify what is the ArrayList since this is a generic class :

    ArrayList<Integer> lista = new ArrayList<>(); // creando el ArrayList
    lista.add(1);
    lista.add(2);
    
    ArrayList<String> lista2 = new ArrayList<>(); // creando un segundo ArrayList
    lista2.add("cadena 1");
    lista2.add("cadena 2");
    
    // tambien lo puedes hacer con un objeto que hallas creado
    class Student{
        private String name;
        public Student(String name){
            this.name = name;
        }
    }
    
    // en otro fichero .java
    ArrayList<Student> estudiantes = new ArrayList<>();
    estudiantes.add(new Student("Juan"));
    estudiantes.add(new Student("Pepe"));
    

    I hope this helps you

        
    answered by 13.11.2018 в 20:30