0

I read in a CSV file using OpenCSV to an ArrayList<String[]> called myCSVEntries. That works as expected, resulting with each line of the file being a String[] inside myCSVEntries.

Now I would like to extract certain lines from myCSVEntries and group them in a particular way inside a new variable called, say, myGroupedEntries. The idea is this: Suppose the file had certain lines that belong to group 1, and some that belong to group 2, and some other lines that are not part of the groups. Each group has, say, 2 line (i.e. two String[]'s).

Ultimately I would like to be able access elements along the lines:

myGroupedEntries.get(ith group.get(nth line.get(kth item in line))

I have declared myGroupedEntries as ArrayList>. During the processing I use a tempArray declared as ArrayList<String[]>. tempArray collects the relevant lines for group 1.

When that is done, I apply myGroupedEntries.add(tempArray).

I then reset tempArray as new, and start again, but now to fill it with the group 2 elements. When that is done, I again apply myGroupedEntries.add(tempArray).

However, instead of myGroupedEntries having a structure like

myGroupedEntries

  - Group1
      - group1 line 1
      - group1 line 2
  - Group2
      - group2 line 1
      - group2 line 2

instead it has a structure more like

myGroupedEntries

  - Group1
      - group1 line 1
  - Group2
      - group1 line 2
  - Group3
      - group2 line 1
  - Group4
      - group2 line 2

I am guessing that adding the tempArray declared as ArrayList<String[]> is causing this, but I can't see my way around getting the structure that I was trying for.

... not even sure if this is possible.

Any suggestions would be appreciated

The code looks a bit like this

myGroupedEntries = new ArrayList<ArrayList<String[]>>();    
ArrayList<String[]> tempArrList = new ArrayList<String[]>();    
int iCurrentRow = 0;

for( String[] s: myCSVEntriesList)
{
    ... various checks/tests

    ... the file has some lines that begin with "<%Row:xx", 
    where "xx" is a number indicating the "group" or "set"
    that line of the file belongs to.

    // see if line is a row, and if it is, which row-group it belongs to

    int iRowLoc = s[0].indexOf("<%Row:");
    if(iRowLoc > -1)
    {
        int iRowCLoc = s[0].indexOf(",");
        int iRow = Integer.parseInt( s[0].substring(iRowLoc+6, iRowCLoc).toString() );

        //
        // then see if we are still processing the "same Row's attributes"
        //

        if( iRow != iCurrentRow)
        {
            //
            // then have a new row-set to process
            //

            if( tempArrList.size() == 0 )
            {
                //
                // then it's the first "row-set"
                //
                // ... so get it started with with the first row for this row-set

                tempArrList.add(s);
                iCurrentRow = iRow;
            }
            else
            {
                //
                // there is an existing row-set (i.e. the "previous" row-set)
                // so must update the master table with the previous row-set
                // and create a new temp row-set package
                //

                myGroupedEntries.add(tempArrList);

                tempArrList = new ArrayList<String[]>();

                tempArrList.add(s);

                iCurrentRow = iRow;

            } // if( tempArrList == null)

        }
        else
        {
            //
            // then it's part of the same row-set
            // so just add the current s
            //

            tempArrList.add(s);

        } // if( iRow > iCurrentRow)

    } // if(iRowLoc > -1)

    //
    // do final test to see if there is a last row-set that needs to be added
    //

    if( tempArrList.size() != 0 )
    {
        myGroupedEntries.add(tempArrList);

        tempArrList = new ArrayList<String[]>();
    }

} // for( String[] s: myCSVEntriesList)
3
  • A conceptual question, what does your CSV look like. To me it seems like your groups carry an identifier or distinguishable attribute of some sorts. Is that the case? Dec 6, 2013 at 7:33
  • Many thanks to Eric and Andru for your edits, I did not realise that the line returns did not make into the post, and I don't have permissions for edit.
    – DrOli
    Dec 6, 2013 at 8:18
  • You are welcome. While the answer solves your problem, I think your design can be improved quite a bit. I would suggest using the Google Guava library and storing your groups in a Dictionary for fast access. It will require that you write some code to compare your groups but should be a better choice if your software should be reusable. Dec 6, 2013 at 8:37

1 Answer 1

1

Your final test

if( tempArrList.size() != 0 )
{
        myGroupedEntries.add(tempArrList);

        tempArrList = new ArrayList<String[]>();
} 

should be outside of the main for loop. Right now it is executed every time anything has been added to the tempArrList, creating results you are getting.

2
  • Well, I feel a bit daft for such a bush-league error ... that was it. Many thanks
    – DrOli
    Dec 6, 2013 at 8:16
  • BTW, I tried to up vote your answer, but I am not permitted (not sufficient reputation).
    – DrOli
    Dec 6, 2013 at 8:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.