Search code examples
arraylistexport-to-excelnested-loops

Using a nested loop on multiple array lists


So I have 3 array lists and they are all the same size, I'm trying to print them in an excel file which works however the IP and GEO are only returning the first value over and over.

This is my code:

for (String url : IPGrabber.URLArray) {
        Row row = sheet.createRow(rownum++);
        row.createCell(0).setCellValue(url);
        for (String ip : IPGrabber.IPArray) {
            row.createCell(1).setCellValue(ip);
        }
        for (String geo : GEOLookup.GEOArray) {
            row.createCell(2).setCellValue(geo);
        }
}

This is the output in excel

http://www.google.com   199.59.148.82   United States
http://www.google.com   199.59.148.82   United States
http://www.mcbay.net    199.59.148.82   United States
http://www.facebook.com 199.59.148.82   United States
http://twitter.com  199.59.148.82   United States

As you can see the URLArray and IPArray are repeatign same value

Any help would be greatly appreciated attempted many different methods.


Solution

  • You are doing wrong the for loop. You must do something similar to:

    java-like syntax:

    for ( int i = 0; i < IPGrabber.URLArray.size(); i++ )
    {
     Row row = sheet.createRow(rownum++);
     row.createCell(0).setCellValue(IPGrabber.URLArray.get(i));
     row.createCell(1).setCellValue(IPGrabber.IPArray.get(i));
     row.createCell(2).setCellValue(GEOLookup.GEOArray.get(i));
    }
    

    As arrayList have same size.