How can I fill an ArrayList with an object of an already created class

0

I tell you that I have two classes, a class of Task type and another of type Person, within the attributes of type person, has an ArrayList of Task type, when I create an object of Task type, and when filling this object with its attributes.

     public class Persona {

private String nombre;
private String apellido;
private String clave;
private String usuario;
private ArrayList<Tareas> tareaTrabajo;

What I want is to add to the object of type person, more objects in your ArrayList of Task type.

example:

Tarea jugar=new Tarea("pc", "Sims");
Tarea dormir=new Tarea("Casa","2 horas");

ArrayList<Tarea> misTareas=new ArrayList<Tarea>();
misTareas.add(jugar);
misTareas.add(dormir)
Usuario user1=new Usuario("usuario 1", "Javier", "Gallardo", "Informatica","123",misTareas);

// Attempt 1: I get an error

Tarea trabajar=new Tarea("cine","7 horas");

user1.getTareaTrabajo().add(trabajar); 

// Attempt 2: replace everything saved by the newly created ArrayList

Tarea viajar=new Tarea(". . . ",". . . ");

ArrayList<Tareas>tarea1=new ArrayList<>();
tarea1.add(viajar);

user1.setTareaTrabajo(tarea1);

I hope for your help.

    
asked by Javier Gallardo 27.11.2018 в 21:29
source

2 answers

1

I do not know very well what error it gives you, since you do not mention anything about the output. In principle, the only thing I have seen in the code is that when you show the Person class, in principle you have shown us 5 attributes and in the constructor you have 6, I do not know if you have another field as a class variable that you have not shown.

In the rest of the code it seems that you are running the methods well. So, if it is not what I have mentioned above it shows the error that it produces when executing it. Anyway, I'll give you the code of what I've tried: Creation of tasks

//Creacion de Tareas
        Tarea jugar = new Tarea ("pc" , "1h");
        Tarea trabajar = new Tarea ("programar" , "7h");
        Tarea dormir = new Tarea ("dormir" , "6h");

Creating the ArrayList

//ArrayList de tareas
ArrayList<Tarea> tareasProgramadas = new ArrayList<Tarea>();

Person Creation.

//Creacion de persona
Persona persona2 = new Persona("Luis" , "Gallardo" , "usuario2" , "123", tareasProgramadas);

If you want to add a second ArrayList to the one you have already defined in the Person class. You should go through the new ArrayList and go running the add method of the ArrayList that you get from getTareaTrabajo ().

//Creo un segundo ArrayList, al que le he metido las mismas tareas que el primero por comodidad
ArrayList<Tarea> tareasProgramadas2 = tareasProgramadas;

//For each que recorre tareasProgramadas2 y lo mete en el ArrayList almacenado en personas
        for(Tarea t:tareasProgramadas2){
            persona2.getListaTareas().add(t);
        }
    
answered by 27.11.2018 / 22:27
source
0

The problem in your code is the following:

public class Persona {

.
.
.
private ArrayList<Tareas> tareaTrabajo;

Where if you look at the arraylist you are defining that it carries objects of type Tareas

in the code that you put later

ArrayList<Tarea> misTareas=new ArrayList<Tarea>();

you are defining that it carries objects of type Tarea

As Tarea and Tareas are not the same, that's why it fails.

    
answered by 28.11.2018 в 02:00