0

我有一个数组列表:

private static ArrayList<String> lista;

static void fileReading() {

inp = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(inFileNev), "ISO8859-1")));

            String sor;
            while ((sor = inp.readLine()) != null) {

               lista.add(sor);
               lista.add(System.getProperty("line.separator"));
}

我需要其中的字符串,而不是整行(如“pakcage”和“asd;”)。我尝试了这些,但这些都不起作用:

String[] temp=null;
for(String s : lista) {
   temp = s.split("\\W+"); 
} 
System.out.println(temp);

我得到:[Ljava.lang.String;@6ef53890 如果我在 for 中写 println,我得到的结果是一样的,只是更多。

如果我使用这个:

   String str ="" ;
    for(int i=0; i<lista.size(); i++){
     str+=lista.get(i)+" ";

  String[] temp = new String[str.length()];
  for(int i=0; i < str.length(); i++) {
       temp[i]=str.valueOf(i);
       System.out.println(temp[i]);         
  }

我只得到数字,不知道如何从 str 获取字符串。我需要知道字符串索引,然后再替换它们。

4

1 回答 1

0

In this example

String[] temp=null;
for(String s : lista) {
    temp = s.split("\\W+"); 
} 
System.out.println(temp);

Trying to print an object will result in invoking its toString() method. In fact you are not printing any String value, but an array of Strings, which is an object.

In your second snippet str.valueOf(i); is returning a "string representation of the int argument."

What you probably want to do is

  1. Split your elements into some table, like you did here temp = s.split("\\W+");
  2. And then iterate through the array using foreach:

Which would look like

for (String s : yourStringArray) {
    System.out.println(s);
}

Hope this helps... after almost a year ;)

于 2013-10-08T14:31:30.927 回答