-2

Using a list of strings, I am trying to match the string in a excel sheet and add the cell elements in the inner list. Adding the inner list in the outer list using a loop. Please refer the code below

public static List<ArrayList<String>> getKeywords(List<String> testCaseIdList, String fileName, String sheetName){

        try {

            ArrayList<String> listTestSteps = new ArrayList<String>();
            List<ArrayList<String>> listTestCases = new ArrayList<ArrayList<String>>(0);
            Sheet sheetKW = ReadExcelFile.readExcel(ST_KEYWORDS);
            String columnValue = null;
            int matchFlag, addListFlag = 0;

            for(String testCaseId : testCaseIdList) {
                Iterator<Row> rowIterator = sheetKW.rowIterator();
                listTestSteps.clear();
                while(rowIterator.hasNext()) {
                    Row rowNext = (Row) rowIterator.next();
                    Iterator<Cell> cellIterator = rowNext.cellIterator();
                    matchFlag = 0;
                    addListFlag = 0;
                    //listTestSteps.clear();
                    while(cellIterator.hasNext()) {
                        Cell nextCell = cellIterator.next();
                        columnValue = nextCell.getStringCellValue();
                        //System.out.println("Column value " +columnValue);
                        if((columnValue.equalsIgnoreCase(testCaseId)) && (columnValue != "" )) {
                            matchFlag = 1;
                        }
                        if(matchFlag == 1 && columnValue != "") {
                            listTestSteps.add(columnValue);
                            addListFlag = 1;  

                            System.out.println("Add Value : "+columnValue);
                        }
                    }
                    if((listTestSteps.isEmpty() == false) && (addListFlag == 1)) {
                        System.out.println("Adding to the Main list");
                        listTestCases.add(listTestSteps);
                        //listTestCases.forEach(System.out::println);
                    }
                }
            }
            //listTestSteps.forEach(System.out::println);

            // Return ArrayList of ArrayLists
            return listTestCases;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

I am getting the output as

[TC_0003, login, createUser, deleteUser]
[TC_0003, login, createUser, deleteUser]

Firstly added list listTestSteps is getting replaced by the last iteration list.

Expected output is [[TC_0002, login, createUser, deleteUser, newUser], [TC_0003, login, createUser, deleteUser]] Something is wrong for sure. Any help will be appreciated.

avirup
  • 1,531
  • 2
  • 12
  • 11

2 Answers2

2

Using the

   listTestSteps.clear();

instruction in the loop lets you use always the same list, so in every iteration you just empty and refill the same list and add it to the outer list. For this reason the outer list will at the end contain x entries pointing always at the same list, which is filled with the data you put there in last iteration.

So you just have to do

   ArrayList<String> listTestSteps = new ArrayList<String>();

instead of clearing the list

Massimo Petrus
  • 1,881
  • 2
  • 13
  • 26
  • Lovely. I am getting expected results. But just for the clarification - instead of clearing the list am I initiating a new Arraylist everytime ? – avirup Dec 08 '16 at 11:01
  • 1
    Yes. Otherwise you'll add always, iteration by iteration, the same object, which each time is cleared and refilled with new data – Massimo Petrus Dec 08 '16 at 11:03
1

The problem is you add the reference of listTestSteps to listTestCases and then in the next loop you clear the listTestSteps, but the cleared list is still referenced in listTestCases. So would suggest using the answer to Add an object to an ArrayList and modify it later to ensure that both lists are resolved properly.

Community
  • 1
  • 1
ollowain
  • 33
  • 7