Search code examples
javalistarraylistcollectionssublist

Create subarraylist with condition


i have a problem to get SubarrayLists. I have a Class Accgy. Its including 4 Cells of an Excelsheet (Cell y,x,z ,acc).

    public class Accgy {

        Cell y;
        Cell x;
        Cell z;
        Cell acc;

        public Accgy(Cell x, Cell y, Cell z, Cell acc) {
            this.y = y;
            this.x = x;
            this.z = z;
            this.acc = acc;
        }

        Cell getCellz() { return z; }
        Cell getCelly() { return y; }
        Cell getCellx() { return x; }
        Cell getCellacc() { return acc; }

    }

In the Excelsheet A x,y and z are one Row. In the ArrayList accgy there are all values of the Excelsheet

I have values from an another Excelsheet B in the Arraylist maxPerRange. In this Arraylist there are only the highest values of the Excelsheet B. Both Excelsheets have the same number of Rows.

I want to create an Arraylist which do subArraylists of the ArrayList accgywith the start of index 1590 and end accgy.size()-700. But with the condition that if the values of the ArrayList accgyand rowIndex and the values of the ArrayListmaxPerRange therowIndexare the same, it should divide the ArrayList in subArrayLists.

I want in the subArraylist all values of the ArrayList accgy between the rowIndex of maxPerRange. So the rowIndex of maxPerRange should be like borders. The values in the subArraylist should be from one maxPerRange till the next maxPerRange.

I hope it was clear the explain my problem.

Here is the Code what i have till now.

I think one mistake is partitions1.add(accgy.subList(i, accgy.size() - 700));

But i am not sure. I hope someone can help me.

        List<List<Accgy>> partitions1 = new ArrayList<>();

        for (int i = 1590; i < accgy.size() - 700; i++) {
            for (int j = 0; j < maxPerRange.size(); j++) {
                if (accgy.get(i).getCellx().getRowIndex() == maxPerRange.get(j).getRowIndex()) {
                    partitions1.add(accgy.subList(i, accgy.size() - 700));
                }
            }
        }

        for (List<Accgy> c : partitions1) {
            for (Accgy g : c) {
                System.out.println(g.getCellx().getNumericCellValue());
                System.out.println(g.getCellx().getRowIndex());

            }
        }

Solution

  • I have fixed my Problem. With this code i have the same numbers of subarrayLists like the size of the ArrayList maxPerRange and the values of the ArrayList maxPerRange are the borders when it should do a subarrayList.

        List<List<Accgy>> partitions1 = new ArrayList<>();
            for (int i = 1590; i < accgy.size()-700; i++) {
                    for (int j=0;j< maxPerRange.size();j++) {
                        if (accgy.get(i).getCellx().getRowIndex() == maxPerRange.get(j).getRowIndex()) {
                            partitions1.add(accgy.subList(i,  maxPerRange.get(j+1).getRowIndex()));
                        }else if(accgy.get(i).getCellx().getRowIndex() >= maxPerRange.get(maxPerRange.size()-1).getRowIndex()){
                                break;
                            }
    
                    }
            }
    
    

    I hope it helps for someone who has the same problem.