Java 如何控制嵌套ArrayList的结构
我使用Java 如何控制嵌套ArrayList的结构,java,arraylist,Java,Arraylist,我使用OpenCSV将CSV文件读入名为myCSVEntries的ArrayList。这与预期的一样,导致文件的每一行都是myCSVEntries内部的String[] 现在我想从myCSVEntries中提取某些行,并将它们以特定的方式分组到一个名为myGroupedEntries的新变量中。想法是这样的:假设文件中有属于组1的某些行,一些属于组2的行,还有一些不属于组的其他行。例如,每组有两行(即两个字符串[]) 最终,我希望能够访问以下内容: myGroupedEntries.get(第i
OpenCSV
将CSV
文件读入名为myCSVEntries
的ArrayList
。这与预期的一样,导致文件的每一行都是myCSVEntries
内部的String[]
现在我想从myCSVEntries
中提取某些行,并将它们以特定的方式分组到一个名为myGroupedEntries
的新变量中。想法是这样的:假设文件中有属于组1的某些行,一些属于组2的行,还有一些不属于组的其他行。例如,每组有两行(即两个字符串[]
)
最终,我希望能够访问以下内容:
myGroupedEntries.get(第i个group.get(第n行.get(第k行中的项目))
我已将myGroupedEntries
声明为ArrayList>。在处理过程中,我使用声明为ArrayList
的tempArray
收集组1的相关行
完成后,我应用myGroupedEntries.add(tempArray)
然后我将tempArray
重置为新的,然后重新开始,但现在要用第2组元素填充它。完成后,我再次应用myGroupedEntries.add(tempArray)
但是,与具有如下结构的myGroupedEntrie相比
myGroupedEntries
- Group1
- group1 line 1
- group1 line 2
- Group2
- group2 line 1
- group2 line 2
- Group1
- group1 line 1
- Group2
- group1 line 2
- Group3
- group2 line 1
- Group4
- group2 line 2
相反,它的结构更像
myGroupedEntries
- Group1
- group1 line 1
- group1 line 2
- Group2
- group2 line 1
- group2 line 2
- Group1
- group1 line 1
- Group2
- group1 line 2
- Group3
- group2 line 1
- Group4
- group2 line 2
我猜添加声明为ArrayList
的tempArray
是造成这种情况的原因,但我看不到如何获得我尝试的结构
…甚至不确定这是否可能
如有任何建议,将不胜感激
代码看起来有点像这样
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)
myGroupedEntries=newArrayList();
ArrayList tempArrList=新建ArrayList();
int iCurrentRow=0;
对于(字符串[]s:myCSVEntriesList)
{
…各种检查/测试
…文件中有些行以“您的最终测试”开头
if( tempArrList.size() != 0 )
{
myGroupedEntries.add(tempArrList);
tempArrList = new ArrayList<String[]>();
}
if(tempArrList.size()!=0)
{
myGroupedEntries.add(tempArrList);
tempArrList=newarraylist();
}
应该在主for
循环之外。现在,每次将任何内容添加到临时列表中时,都会执行该循环,从而创建您得到的结果。一个概念性问题,您的CSV看起来像什么。在我看来,您的组似乎带有某种标识符或可区分属性。是ca吗se?非常感谢Eric和Andru的编辑,我没有意识到行返回没有进入帖子,我也没有编辑权限。欢迎您。虽然答案解决了您的问题,但我认为您的设计可以改进很多。我建议使用Google Guava库并将您的组存储在字典中,以便快速访问。这需要您编写一些代码来比较您的组,但是如果您的软件应该是可重用的,那么这应该是一个更好的选择。好吧,对于这样一个布什联盟的错误,我觉得有点愚蠢……就是这样。很多人感谢SBTW,我试图投票支持您的答案,但我不被允许(没有足够的声誉)。