data sum of a 4 array without reperting

1

good I want to add 4 data if it is in the array a b c d but if you add once and do not add again look at a = 36 data, b = 28, c = 7, d = 3; they are the people who have an amount; if I join in an interacion and the code repeats I should not add in another interacion  the iterations would be the following ones

add a data:
a + b + c + d
a + b + c
a + c + d
b + c + d
d + a + b
a + b
a + c
a + d
b + c
b + d
c + d
suppose I have 5 employees

employee 1 would have to leave = 27
employee 2 = 4
employed 3 = 21
Employee 4 = 2
Employee 5 = 1
but I get it like this:
Employee 1 = 27
empleado1 = 21
Employee 1 = 15
Employee 1 = 11
and so I want to ignore if I already add

for (int i = 0; i < list0706a.size(); i++) {

            double suma = 0;

            for (int j = 0; j < list0706b.size(); j++) {
                if (list0706b.get(j).getCODIGO() == list0706a.get(i).getCODIGO()) {
                    for (int a = 0; a < list0706c.size(); a++) {
                        if (list0706c.get(a).getCODIGO() == list0706b.get(j).getCODIGO()) {
                            for (int d = 0; d < list0706d.size(); d++) {
                                if (list0706d.get(d).getCODIGO() == list0706c.get(a).getCODIGO()) {
                                    //Encontre =  1234;
                                    suma = (listh0706a.get(i).getRESULTADO()
                                            + listh0706b.get(j).getRESULTADO()
                                            + listh0706c.get(a).getRESULTADO()
                                            + listh0706d.get(d).getRESULTADO());
                                    pw.println(String.valueOf("0" + list0706d.get(i).getCODIGO_DOC()
                                            + "|" + list0706d.get(i).getCEDULA() + "|"
                                            + "0706" + "|"
                                            + df.format(suma) + "|"
                                            + df.format(suma) + "|"));
                                }

                            }

                        }
                    }

                }
            }
        }

So I did the for but when I continue it adds the previous ones

    
asked by Dalstron 01.12.2018 в 04:41
source

1 answer

1

The first thing is that the code is very confusing. The second is that, unless there is something that you do not know or see, this code is extremely inefficient.

I see that you are using 4 lists of an object with two attributes ( code and result ) These lists are called:

  • list0706a
  • list0706b
  • list0706c
  • list0706d

If the lists are sorted (the first record in all the lists belongs to the same employee, the second to the second, etc. and then the size of all the lists are the same size)

You can solve it with a simple loop

for (int i = 0; i < list0706a.size(); i++) {
  suma = (listh0706a.get(i).getRESULTADO()
         + listh0706b.get(i).getRESULTADO()
         + listh0706c.get(i).getRESULTADO()
         + listh0706d.get(i).getRESULTADO());
  pw.println(String.valueOf("0" + list0706d.get(i).getCODIGO_DOC()
             + "|" + list0706d.get(i).getCEDULA() + "|"
             + "0706" + "|"
             + df.format(suma) + "|"
             + df.format(suma) + "|"));
}

and so you go from a cost code n ^ 4 to cost n.

If the lists are out of order I would opt for a hashmap (change employee for your object)

  Map<Employee, Integer> map = new HashMap<Employee, Integer>();
    for(int i=0;i<list0706a.size();i++)
    {
        if(map.containsKey(list0706a.get(i)))
        {
            map.put(list0706a.get(i),map.get(list0706a.get(i)+list0706a.get(i).getRESULTADO()));
        }
        else
        {
            map.put(list0706a.get(i),map.get(list0706a.get(i).getRESULTADO()));
        }
    }
    for(int i=0;i<list0706b.size();i++)
    {
        if(map.containsKey(list0706b.get(i)))
        {
            map.put(list0706b.get(i),map.get(list0706b.get(i)+list0706b.get(i).getRESULTADO()));
        }
        else
        {
            map.put(list0706b.get(i),map.get(list0706b.get(i).getRESULTADO()));
        }
    }
    for(int i=0;i<list0706c.size();i++)
    {
        if(map.containsKey(list0706c.get(i)))
        {
            map.put(list0706c.get(i),map.get(list0706c.get(i)+list0706c.get(i).getRESULTADO()));
        }
        else
        {
            map.put(list0706c.get(i),map.get(list0706c.get(i).getRESULTADO()));
        }
    }
    for(int i=0;i<list0706d.size();i++)
    {
        if(map.containsKey(list0706d.get(i)))
        {
            map.put(list0706d.get(i),map.get(list0706d.get(i)+list0706d.get(i).getRESULTADO()));
        }
        else
        {
            map.put(list0706d.get(i),map.get(list0706d.get(i).getRESULTADO()));
        }
    }

    for(PlayerModel i=0:map.keySet())
    {
        pw.println(String.valueOf("0" + i.getCODIGO_DOC()
                + "|" + i.getCEDULA() + "|"
                + "0706" + "|"
                + df.format(map.get(i)) + "|"
                + df.format(map.get(i)) + "|"));
    }
}

And even though this code does not like me at all, besides that it is expensive to understand, its cost is 5N

I recommend that you find, if possible, some way that you did not receive 4 different lists. Approach the employee object (or whatever it is called) add parameters for each possible value that you may have or change get RESULT to return an integer / doubles list

    
answered by 14.12.2018 в 14:59