Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中将字符串数组拆分为单独的数组_Java_Arrays_String_Split - Fatal编程技术网

如何在Java中将字符串数组拆分为单独的数组

如何在Java中将字符串数组拆分为单独的数组,java,arrays,string,split,Java,Arrays,String,Split,我正在尝试将阵列拆分为单独的阵列 我有一串单词。我把单词分成一个数组。现在我尝试将单词数组拆分为它们自己的单独数组 例如: string = "This is a string"; words = [This, is, a, string] 我希望我的输出是: [This], [is], [a], [string] 以下是我目前掌握的情况: String[] words = string.split(" "); String wordsArray = Arrays.toString(

我正在尝试将阵列拆分为单独的阵列

我有一串单词。我把单词分成一个数组。现在我尝试将单词数组拆分为它们自己的单独数组

例如:

string = "This is a string";
words = [This, is, a, string]
我希望我的输出是:

[This], [is], [a], [string]
以下是我目前掌握的情况:

String[] words = string.split(" ");    
String wordsArray = Arrays.toString(words); 
System.out.println(wordsArray.split(" ").toString());
这是我的输出:

[Ljava.lang.String;@63947c6b
你可以这样做

words = "This is a string";
String[] wordsArray=words.split(" ");
Arrays.stream(wordsArray).map(s -> s="["+s+"]").forEach(System.out::println);
你可以这样做

words = "This is a string";
String[] wordsArray=words.split(" ");
Arrays.stream(wordsArray).map(s -> s="["+s+"]").forEach(System.out::println);

以下是构建字符串[]的一种方法:


正如您所注意到的,不能简单地将数组传递给println,因为其默认的toString方法不会显示其元素。因此,请使用Arrays.toString或Arrays.deepToString方法打印数组元素。

以下是构建字符串[][]的一种方法:


正如您所注意到的,不能简单地将数组传递给println,因为其默认的toString方法不会显示其元素。因此,请使用Arrays.toString或Arrays.deepToString方法打印数组元素。

您的意思是这样的,使用打印嵌套数组吗

String string = "This is a string";
System.out.println(string);

String[] words = string.split(" ");
System.out.println(Arrays.toString(words));

String[][] wordsArray = new String[words.length][];
for (int i = 0; i < words.length; i++)
    wordsArray[i] = new String[] { words[i] };
System.out.println(Arrays.deepToString(wordsArray));
如果您只想构建列出的字符串,这是一种更简单的方法:

String string = "This is a string";
StringJoiner joiner = new StringJoiner("], [", "[", "]");
for (String word : string.split(" "))
    joiner.add(word);
System.out.println(joiner.toString());
输出

[This]、[is]、[a]、[string] 或在使用流的单个语句中使用相同的语句:

System.out.println(Pattern.compile(" ")
                          .splitAsStream("This is a string")
                          .collect(Collectors.joining("], [", "[", "]")));
或者你可以作弊,然后这样做:

String string = "This is a string";
System.out.println("[" + string.replaceAll(" ", "], [") + "]");

您的意思是像这样,使用打印嵌套数组吗

String string = "This is a string";
System.out.println(string);

String[] words = string.split(" ");
System.out.println(Arrays.toString(words));

String[][] wordsArray = new String[words.length][];
for (int i = 0; i < words.length; i++)
    wordsArray[i] = new String[] { words[i] };
System.out.println(Arrays.deepToString(wordsArray));
如果您只想构建列出的字符串,这是一种更简单的方法:

String string = "This is a string";
StringJoiner joiner = new StringJoiner("], [", "[", "]");
for (String word : string.split(" "))
    joiner.add(word);
System.out.println(joiner.toString());
输出

[This]、[is]、[a]、[string] 或在使用流的单个语句中使用相同的语句:

System.out.println(Pattern.compile(" ")
                          .splitAsStream("This is a string")
                          .collect(Collectors.joining("], [", "[", "]")));
或者你可以作弊,然后这样做:

String string = "This is a string";
System.out.println("[" + string.replaceAll(" ", "], [") + "]");
产生:

[THis], [is], [an], [example]
产生:

[THis], [is], [an], [example]

如果您想简单地打印它,那么使用streams api非常简单。以下是一个例子:

String output = Arrays.stream("This is a string".split("\\s"))
                      .map(s -> "[" + s + "]")
                      .collect(Collectors.joining(", "));

如果您想简单地打印它,那么使用streams api非常简单。以下是一个例子:

String output = Arrays.stream("This is a string".split("\\s"))
                      .map(s -> "[" + s + "]")
                      .collect(Collectors.joining(", "));
它可以简单地做为:

String string = "This is a string";     
    System.out.println(string);     
    String[] words = string.split(" ");
    System.out.println(Arrays.toString(words));

    String[][] wordsArray= new String[words.length][1];
    for(int i=0;i<words.length;i++){
        wordsArray[i][0]=words[i]; 
    }
    System.out.println(Arrays.deepToString(wordsArray));
它可以简单地做为:

String string = "This is a string";     
    System.out.println(string);     
    String[] words = string.split(" ");
    System.out.println(Arrays.toString(words));

    String[][] wordsArray= new String[words.length][1];
    for(int i=0;i<words.length;i++){
        wordsArray[i][0]=words[i]; 
    }
    System.out.println(Arrays.deepToString(wordsArray));

您的意思是将每个单词放入字符字符串数组?如果它是数组数组,那么您的输出将是:[[This]、[is]、[a]、[string]]。那么您更关心的是数据结构还是输出呢?让您困惑的一半是数组上的toString看起来像[Ljava.lang.String;@63947c6b.如果您使用System.out.printlnArrays.toStringwordsArray.split;您就不会那么困惑了。您的问题还不清楚。您为什么想要一个包含单个元素的数组?您的意思是将每个单词放入一个字符字符串数组中?如果它是一个数组,那么您的输出将是:[[This],[is],[a],[String]].那么您更关心的是数据结构还是输出?让您困惑的一半是数组上的toString看起来像[Ljava.lang.String;@63947c6b.如果你做了System.out.printlnArrays.toStringwordsArray.split;你就不会那么困惑了。你的问题还不清楚。你为什么想要一个包含单个元素的数组?最后一个…LOL:DLast one…LOL:d