如何增加字符串数组的大小以在arraylist末尾添加额外的数据

时间:2015-02-26 14:06:33

标签: java string csv arraylist

我正在填充CSV文件并希望存储在arraylist中。 在CSV文件中,某些记录在最后一个字段中具有空值。如果它们是空白的,则可以轻松填充数据。但是当最后一个字段为空时。 抛出异常:

  

无法读取CSV文件:java.lang.ArrayIndexOutOfBoundsException:26

csv文件中有27个字段,异常出现在最后一个字段中。有没有办法增加这些记录的String数组的大小,所以我可以达到26个字段,然后在第27个位置插入空白?

代码段是::

String csvFile = "State.txt";
     ArrayList<StateRecord> state=new ArrayList<StateRecord>();
     SimpleDateFormat dateFormat = new SimpleDateFormat("mm/DD/yyyy");


BufferedReader br = new BufferedReader(new FileReader(csvFile));

String line = " ";
while ((line= br.readLine()) != null) 

 {
        lineNumber++;

String[] st=line.split("\\|");

String[] t;

StateRecord stItem = new StateRecord();
stItem.setstateISONumericId(st[0]);

stItem.setstateISOName(st[1]);

stItem.setstateISOAbbrName(st[2]);
stItem.setstateISOVeryAbbrName(st[3]);
        stItem.setstateEffectiveDate(dateFormat.parse(st[4]));
        stItem.setstateDivisionTypeCode(st[5]);
        stItem.setstateLastTransDate(dateFormat.parse(st[6]));
        stItem.setstateLastTransCode(st[7]);


}
state.add(StItem);

1 个答案:

答案 0 :(得分:0)

尝试以下逻辑。就像在if if语句中包含你的逻辑“if(line.split(”\ |“)。length&gt; 1)”。这将允许您跳过所有空列

  while ((line= br.readLine()) != null) 

  {
      if(line.split("\\|").length>1) { // This logic will help you to skip null columns
              lineNumber++;
              String[] st=line.split("\\|");
               ---------------------------
                       your logic
               ---------------------------

 }