4

Add ArrayList to another ArrayList

 2 years ago
source link: https://www.codesd.com/item/add-arraylist-to-another-arraylist.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Add ArrayList to another ArrayList

advertisements

I have two classes:

Products:

01; Desinfectante
02; Aerosol
03; Limpia Vidrio
04; Desengrasante
05; Mata mosquitos
06; Mata cucarachas
07; Aceite en aerosol

Instructions:

01;1;Elevar la masa hasta llegar a tal punto;0;10
01;1;Mezclar este material con anterior;1;15
01;2;Relevar;2;5
01;3;Llevar;00;0
02;1;Descripcion;7;2
02;2;Descripcion;6;2
02;2;Descripcion;00;0
03;1;Descripcion;1;1
03;1;Descripcion;2;9
03;2;Descripcion;00;0
03;3;Descripcion;5;2
03;4;Descripcion;6;2
03;4;Descripcion;3;10
04;1;Descripcion;00;0
04;2;Descripcion;1;2
04;3;Descripcion;1;0
04;3;Descripcion;2;2
04;3;Descripcion;3;2
04;4;Descripcion;7;1
04;4;Descripcion;6;2
05;1;Descripcion;7;20
05;1;Descripcion;6;9
05;2;Descripcion;00;0
05;3;Descripcion;1;2
05;3;Descripcion;2;10
06;1;Descripcion;2;12
06;1;Descripcion;4;1
06;1;Descripcion;6;8
06;2;Descripcion;5;4
06;2;Descripcion;7;2
07;1;Descripcion;1;12
07;1;Descripcion;2;2
07;2;Descripcion;3;19
07;2;Descripcion;4;4
07;2;Descripcion;00;2
07;2;Descripcion;5;12

The thing is this: i have to insert the instructions ArrayList into the Products. The link between them is the first number, that is the code of the product.

I tried two things, the first one:

public static ArrayList<Productos> InsertInstInProd(ArrayList<Instrucciones> instructions, ArrayList<Productos> products)
{
    for (int i = 0; i < products.size()-1; i++)
    {
        int n = 0;
        for (int j = 0; j < instructions.size()-1; j++)
        {
            int first = products.get(i).getNumero();
            int second = instructions.get(j).getCodProd();

            if (first == second)
            {
                products.get(i).getInstr().get(n).setCodIns(instructions.get(j).getCodIns());
                products.get(i).getInstr().get(n).setCodProd(instructions.get(j).getCodProd());
                products.get(i).getInstr().get(n).setDescr(instructions.get(j).getDescr());
                products.get(i).getInstr().get(n).setMat(instructions.get(j).getMat());
                products.get(i).getInstr().get(n).setMatNec(instructions.get(j).getMatNec());

                n++;
            }
        }
        n = 0;
    }

The second one:

public static ArrayList<Productos> InsertInstInProd(ArrayList<Instrucciones> instructions, ArrayList<Productos> products)
{
    for (int i = 0; i < products.size()-1; i++)
    {
        int n = 0;
        for (int j = 0; j < instructions.size()-1; j++)
        {
            int first = products.get(i).getNumero();
            int second = instructions.get(j).getCodProd();

            if (first == second)
            {
                products.get(i).setInstr(instructions);
                n++;
            }
        }
        n = 0;
    }

    return products;
}


You are getting NullPointerException because of

     products.get(i).getInstr().get(n).setCodIns(instructions.get(j).getCodIns());

You are not checking whether the list products.get(i).getInstr() has elements or not. When the list is empty and when you are accessing it as products.get(i).getInstr().get(0) it's throwing you NullPointerException because trying to get the first element of an empty list. So before you do this operation, make sure that products.get(i).getInstr() is not empty.

If they are of same type, you can directly add the whole arraylist :

   products.get(i).getInstr().addAll(instructions); // again make sure that is not empty.

If you just want to replac, use :

    products.get(i).setInstr(instructions.get(j));


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK