简体   繁体   English

字符串到字符串数组Java

[英]String to String Array Java

I have a big problem. 我有一个大问题。 I have just try to search a solution, but i don't have find any solution. 我只是尝试搜索解决方案,但我没有找到任何解决方案。 i have this code 我有这个代码

public static String getString() throws IOException {

    String content = null;
    File folder = new File("C:\\Soluzioni.txt");

    content = FileUtils.readFileToString(folder) + "\n";
    String remainingString = content.substring(content.indexOf("["),
            content.lastIndexOf("]") + 1);
    System.out.println(remainingString);
    return remainingString;

}

And this is ok. 没关系。 (For clarity) (为清楚起见)

OUTPUT :[40,-13,-6,-7,-4] [28,-40,45,-29,37] [-43,19,-24,-9,-45] [26,-41,-28,-16,44]

My problem is now: 我的问题是:

public static String[] arg() throws IOException {
    String[] strArray = { getString() };
    System.out.println(strArray);
    return strArray;
}

when i print strArray, i have an error (ECLIPSE show me this: [Ljava.lang.String;@796686c8). 当我打印strArray时,出现错误(ECLIPSE向我显示:[Ljava.lang.String; @ 796686c8)。 I would need my string (remainingString) to become an array of strings(strArray), but that maintained the same format, that is, it was always OUTPUT :[40,-13,-6,-7,-4] [28,-40,45,-29,37] [-43,19,-24,-9,-45] [26,-41,-28,-16,44] but with array format. 我需要我的字符串(remainingString)成为字符串数组(strArray),但保持相同的格式,也就是说,它始终是OUTPUT :[40,-13,-6,-7,-4] [28,-40,45,-29,37] [-43,19,-24,-9,-45] [26,-41,-28,-16,44]但使用数组格式。 Thank you very much! 非常感谢你!

Arrays are objects too in Java, but they don't override Object 's toString() method , which is responsible for the output: [Ljava.lang.String;@796686c8 数组在Java中也是对象,但它们不会覆盖ObjecttoString()方法 ,该方法负责输出: [Ljava.lang.String;@796686c8

In other words, this method returns a string equal to the value of: 换句话说,此方法返回的字符串等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode()) 

Use Arrays.toString to generate the expected array output: 使用Arrays.toString生成预期的数组输出:

System.out.println(Arrays.toString(strArray));

You have to use Arrays.toString() method which returns a string representation of the contents of the specified array. 您必须使用Arrays.toString()方法,该方法返回指定数组内容的字符串表示形式。
Change your system out to System.out.println(Arrays.toString(strArray)); 将您的系统更改为System.out.println(Arrays.toString(strArray));

System.out.println(strArray); will print the Object toString value getClass().getName() + '@' + Integer.toHexString(hashCode()) 将打印对象的toString值getClass().getName() + '@' + Integer.toHexString(hashCode())

That's why you are getting [Ljava.lang.String;@796686c8) . 这就是为什么要获取[Ljava.lang.String;@796686c8)

Please refer http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html for more information. 有关更多信息,请参考http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html

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

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