简体   繁体   English

Java字符串由逗号分隔

[英]Java String split by comma

I have this code down below that gives me this output 我在下面有这个代码给了我这个输出

1,2,3,4,3,4,5,4,3,5,3,4,5,5,4,64,
[Ljava.lang.String;@3e25a5
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f
[I@19821f

The input.txt file contains 1,2,3,4,3,4,5,4,3,5,3,4,5,5,4,64, input.txt文件包含1,2,3,4,3,4,5,4,3,5,3,4,5,5,4,64,

The code is this. 代码是这样的。 It is clear there is a simple error in splitting but I find it hard to find what. 很明显,分裂存在一个简单的错误,但我发现很难找到什么。

public static void main(String[] args) {
    // TODO Auto-generated method stub



    try{
        FileInputStream fstream = new FileInputStream("input.txt");
        DataInputStream  dat = new DataInputStream (fstream);
        BufferedReader in = new BufferedReader(new InputStreamReader(dat));
        String[] str ;

        int arr[] = new int [100];


        String line;

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

                System.out.println(line);
                str = line.split(",");
                System.out.println(str);

                for(int i = 0 ;i<str.length ; i++)
                {
                    arr[i]= Integer.parseInt(str[i]);
                    System.out.println(arr);
                }
            }

        fstream.close();

    }

    catch(IOException e)
    {
        System.out.print(e);
    }


}

Change this 改变这个

   System.out.println(arr);

to

   System.out.println(arr[i]);

before you were printing an array arr , not the array's elements. 在打印数组arr ,而不是数组的元素。

It looks like you're repeatedly printing the array, which will cause Java to call the array's toString() method, that will give you output like you've provided. 看起来你反复打印数组,这将导致Java调用数组的toString()方法,这将为你提供类似于你提供的输出。

To fix it, try changing your loop to this: 要解决此问题,请尝试将循环更改为:

for(int i = 0 ;i<str.length ; i++)
{
    arr[i]= Integer.parseInt(str[i]);
    System.out.println(arr[i]];
}

To give you a clearer idea what's going on, this program will do something similar: 为了让您更清楚地知道发生了什么,该程序将执行类似的操作:

public class Test {
    public static void main(String[] args) {
        System.out.println(new int[0]);
    }
}

It will output: 它将输出:

[I@164f1d0d

By printing out the array in 通过打印出阵列

 System.out.println(arr);

you effectively print out an Object , which happens to be the Array object. 你有效地打印出一个Object ,它恰好是Array对象。 That has a toString() method, and it tends to print out the Object's Class and the hashcode() reference. 它有一个toString()方法,它倾向于打印出Object的Classhashcode()引用。

Now, since your array is of a primitive type, it looks like this 现在,由于您的数组是原始类型,它看起来像这样

[I@19821f

where each item means something 每个项目意味着什么

[ => "an array of"
I => "primitive integer"
@19821f => "which has a hashcode of 0x19821f"

Note that this is a default convention , meaning that while it is what you are seeing, you can't count on this reading technique with the same sense of the word guarantee. 请注意,这是一个默认约定 ,这意味着虽然它正是您所看到的,但您不能指望这种阅读技术具有相同的单词保证意义。

Like many have mentioned before, you need to print out the element of the array (which won't use this pseudo-object toString() representation, because the element is an integer. 像之前提到的许多人一样,你需要打印出数组的元素(它不会使用这个伪对象toString()表示,因为该元素是一个整数。

remove the unnecessary sysouts to get the exact output. 删除不必要的sysout以获得准确的输出。 and in last sysout print the element not complete array. 并在最后一个sysout打印元素不完整的数组。

str = line.split(",");

如果字符串包含逗号,则不应使用split()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM