2

I'm trying to store data from excel in ArrayLists, which will then be stored in a main ArrayList. The reason being that each row of the excel sheet has data that is particular to that row. I keep having an issue because I believe when I erase the inner ArrayList before looping back, it deletes the data in the master ArrayList as well.

import java.io.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.ArrayList;


public class StoreData {

ArrayList<ArrayList<String>> shapes;
ArrayList<String> innerMembers;
ArrayList<String> test;
int start;
int end; 

public StoreData(){
    this.shapes = new ArrayList<>();
    this.innerMembers = new ArrayList<>();
    this.test = new ArrayList<>();
    start = 0;
    end = 0;
}

public ArrayList<ArrayList<String>> storeValues(String shapeType){

    //get the shape
    switch(shapeType){
        case "A":
            start = 1;
            end = 273;
            break;
        case "B":
            start = 274;
            end = 291;
            break;
        case "C":
            start = 292;
            end = 319;
            break;
        case "D":
            start = 320;
            end = 340;
            break;
        case "E":
            start = 341;
            end = 372;
            break;
        case "F":
            start = 373;
            end = 412;
            break;
        case "G":
            start = 413;
            end = 539;
            break;
        case "H":
            start = 540;
            end = 814;
            break;
        case "I":
            start = 1464;
            end = 1958;  
    }

 try
 {
    FileInputStream x = new FileInputStream(new File("/Users/JohnDoe/Documents/File.xls"));

    //Create Workbook instance 
    Workbook workbook = new HSSFWorkbook(x);

    //Get first/desired sheet from the workbook
    Sheet sheet = workbook.getSheetAt(0);

    //Iterate through each rows one by one
    for (int i = start; i <= end; i ++){

        Row row = sheet.getRow(i++);

        //iterate through each cell in row: max number of cells is 76
        for (int j = 0; j < 77; j++) {

            Cell cell = row.getCell(j);
            cell.setCellType(1);
            innerMembers.add(cell.getStringCellValue());
    }            

    /*******************/
    //Add to master ArrayList and clear inner ArrayList to get repopulated
    /*******************/
    shapes.add(innerMembers);
    innerMembers.clear();
}
x.close();
}
catch (Exception e)
{
    e.printStackTrace();
}

/***TEST STATEMENT***/
System.out.println(shapes.get(8).get(20));
/***TEST STATEMENT***/

return shapes;

}
}
1
  • 2
    You need to create a new ArrayList of innermembers each time you call the storeValues method. The shapes is storing the same innermembers instance and that is when you are clearing the elements from the array list it is reflected the shapes reference array list. Commented Dec 22, 2014 at 4:10

1 Answer 1

3

You might want to try:

shapes.add(new ArrayList<String>(innerMembers)); // shallow copy 
innerMembers.clear();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.