【问题标题】:Splitting ArrayLists into Multiple ArrayLists将 ArrayList 拆分为多个 ArrayList
【发布时间】:2014-08-17 23:44:06
【问题描述】:

我想知道如何将以下数据拆分为多个列表。这是我的输入(来自文本文件,此处重新创建了示例):

aaaa bbbb cccc,ccc,cccc

aaaa-- bbbb

aaaa bbbb cccc-

aaaa bbbb cccc,ccc

aaaa-

aaaa bbbb ccc,cccc,cccc

分隔文本的每个部分是一个空格。我需要制作的代码应该创建三个列表,由文本文件中每个条目的 a、b 和 c 组组成,相对于每一行,同时忽略任何带有“-”的行。所以,我的 3 个数组应该填充如下:

Array1: aaaa, aaaa, aaaa
Array2: bbbb, bbbb, bbbb
Array3: (cccc,ccc,cccc),(cccc,ccc),(ccc,cccc,cccc)

添加了括号以显示第三个数组应包含所有列出的 c 值 a、b 和 c 都包含从文本文件导入的字符串。到目前为止,这是我的代码:

import java.util.*;
import java.io.*;

public class SEED{

public static void main (String [] args){

    try{

        BufferedReader in = new BufferedReader(new FileReader("Curated.txt"));
        String temp;
        String dash = "-";
        int x = 0;
        List<String> list = new ArrayList<String>();
        List<String> names = new ArrayList<String>();
        List<String> syn = new ArrayList<String>();
        List<String> id = new ArrayList<String>();


        while((temp = in.readLine()) != null){

            if(!(temp.contains(dash))){

                list.add(temp);

                if(temp.contains(" ")){

                    String [] temp2 = temp.split(" ");
                    names.add(temp2[0]);
                    syn.add(temp2[1]);
                    id.add(temp2[2]);

                }else{

                    System.out.println(temp);

                }//Close if

                System.out.println(names.get(x));
                System.out.println(syn.get(x));
                System.out.println(id.get(x));

            x++;


            }//Close if


        }//Close while

    }catch (Exception e){

e.printStackTrace();
        System.exit(99);

    }//Close try

}//Close main

}//Close class

但我的输出总是:什么都没有。如何正确地将这些值保存到 3 个单独的 Arrays 或 ArrayLists?

【问题讨论】:

  • 好吧,我会迭代所有元素,我会使用很多 if 和 string 的 contains() 来做到这一点。
  • 谢谢桑托斯先生,我找到了以下错误:java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:604 ) 在 java.util.ArrayList.get(ArrayList.java:382) 在 SEED.main(SEED.java:39)
  • 我也不会在您的异常处理中使用 System.exit(99)

标签: java arrays file text arraylist


【解决方案1】:

您正在引用 list.get(x),但您的 x++ 和您的 list.add 将不同步,如果你读到没有破折号的一行。所以 (x) 不是正确的参考。

你为什么这样做:

String [] temp2 = list.get(x).split(" ");

代替:

String [] temp2 = temp.split(" ");

编辑

试试:

if(!(temp.contains(dash))){

            list.add(temp);

            if(temp.contains(" ")){

                String [] temp2 = temp.split(" ");
                names.add(temp2[0]);
                syn.add(temp2[1]);
                id.add(temp2[2]);
            }else{

                System.out.println(temp);

            }//Close if

        }//Close if

for(int x = 0; x < names.size(); x++) {

            System.out.println(names.get(x));
            System.out.println(syn.get(x));
            System.out.println(id.get(x));
}

【讨论】:

  • 哦,这样会更简单一些。老实说,当我编写代码时,我只是在考虑列表。但是我的 x++ 不是在检查破折号的 if 语句之外,因此它不应该通过 while 循环更新吗?
  • 如果你读到一行带有破折号的行,你并没有将它添加到列表中,但你仍在递增 x。所以当你引用 list.get(x) 时,你会得到一个 indexoutofbounds 异常
  • 这是有道理的,但是当我尝试移动 x++ 语句以使其仅在未检测到破折号时递增时,发生了相同的错误。对不起,如果这些看起来像愚蠢的问题,我接触 CS 已经好几年了,我有点生疏了
  • 啊,我只是想这样做,我为每个数组列表使用了替代循环三个不同的时间,它最终工作了。非常感谢您的帮助!
【解决方案2】:

可能抛出了一个异常而你忽略了它。

把你的渔获改为

}catch (Exception e){
    e.printStackTrace();
    System.exit(99);

}//Close try

【讨论】:

  • 我遇到了 indexoutofbound 异常。但是为什么 - 当我向每个列表添加更多值时,我的 3 个列表没有变大吗?异常在代码第 39 行的索引 1、大小 1 处引发
猜你喜欢
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多