I have a method that has made 4 Array lists of array lists and I am trying to figure out out to return that data. I know it will not let me do more than one return statement, so is there a way to combine the ArrayLists together to return?
public class sys {
public static void main(String[] args)
{
try
{
FileInputStream file = new FileInputStream(new File("data/database.xlsx"));
XSSFWorkbook mainDB = new XSSFWorkbook(file);
int i = 0;
do
{
ArrayList<ArrayList<String>> sheet1 = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> sheet2 = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> sheet3 = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> sheet4 = new ArrayList<ArrayList<String>>();
ArrayList<String> cellArray = new ArrayList<String>();
XSSFSheet sheet = mainDB.getSheetAt(i);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
if(row.getRowNum()==0)
{
continue; //skip the first row
}
else
{
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//switch(cell.getCellType())
{
String c = "";
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
{
c = Integer.toString((int)(cell.getNumericCellValue()));
}
if(cell.getCellType() == Cell.CELL_TYPE_STRING)
{
c = cell.getStringCellValue();
}
cellArray.add(c);
}
}
if (i==0);
{
sheet1.add(cellArray);
}
if (i==1);
{
sheet2.add(cellArray);
}
if (i==2);
{
sheet3.add(cellArray);
}
if (i==3);
{
sheet4.add(cellArray);
}
}
}
return sheet1;
return sheet2;
return sheet3;
return sheet4;
i++;
}
while (i<4);
file.close();
}
catch (FileNotFoundException error1) {
error1.printStackTrace();
}
catch (IOException error2) {
error2.printStackTrace();
}
}
}
If you have a set number of ArrayLists, then the most efficient way to do it would be to create an array of ArraysLists, as such:
ArrayList<ArrayList<String>>[] array = new ArrayList<ArrayList<String>>[] {arrayList1, arrayList2, arrayList3, arrayList4};
or as follows:
ArrayList<ArrayList<String>>[] array = new ArrayList<ArrayList<String>>[4];
array[0] = arrayList1;
array[1] = arrayList2;
array[2] = arrayList3;
array[3] = arrayList4;
EDIT: As comments pointed out, the above is actually not possible! Here's an updated solution:
ArrayList<ArrayList<String>>[] array = (ArrayList<ArrayList<String>>[]) Array.newInstance(arrayList1.getClass(), arrayList1.size());
*assuming arrayList1 is one of the OP's mentioned ArrayLists
public class ArrayListUtils {
public static void main(String[] args) {
ArrayListUtils listUtils = new ArrayListUtils();
ArrayList<String> one = new ArrayList<>();
one.add("A");
one.add("B");
ArrayList<String> two = new ArrayList<>();
two.add("AA");
two.add("BA");
ArrayList<String> three = new ArrayList<>();
three.add("AAA");
three.add("BAA");
ArrayList<String> four = new ArrayList<>();
four.add("AAAA");
four.add("BAAA");
//If you need ArrayList of four ArrayList<String> object
ArrayList<ArrayList<String>> arrayList = listUtils.getArrayList(one, two, three, four);
//If you need List of four ArrayList<String> object
List<ArrayList<String>> list = listUtils.getList(one, two, three, four);
}
/*
ArrayList<String>... strings :: means varargs
*/
public ArrayList<ArrayList<String>> getArrayList(ArrayList<String>... strings) {
ArrayList<ArrayList<String>> list = new ArrayList<>();
list.addAll(Arrays.asList(strings));
return list;
}
public List<ArrayList<String>> getList(ArrayList<String>... strings) {
return Arrays.asList(strings);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.