简体   繁体   English

将数组中的每个元素拆分为多个数组

[英]Split string each element in an array into multiple arrays

I have an array with 2 data fields in each element: 我在每个元素中都有一个包含2个数据字段的数组:

String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};

The first and last names and separated by a tab ("\\t"). 名字和姓氏,并用制表符(“ \\ t”)分隔。 How would I go about splitting them into two arrays, for example: 我将如何将它们分为两个数组,例如:

String[] firstName = {"Bob", "Barbara", "John"};
String[] lastName = {"Marley", "Newton", "Smith"};

I initially tried split("\\t") but that didn't work, and I've tried looking up for similar questions here to no avail. 我最初尝试了split(“ \\ t”),但是没有用,并且我尝试在此处查找类似的问题也无济于事。

One thing to note, I am not using ArrayList. 需要注意的一件事,我没有使用ArrayList。

Any help would be immensely appreciated. 任何帮助将不胜感激。 Thank you in advance. 先感谢您。

Code snippet: 程式码片段:

public static String[] sortNames(String[] info) {

    String[] firstName = new String[info.length];
    String[] lastName = new String[info.length];

    for(int i = 0; i < info.length; i++) {
        firstName[i] = info[i].split("\t");
    }

    return firstName;
}

firstName[i] = info[i].split("\\t"); is assign an array to an element,it will cause compile failure. 为元素分配数组,将导致编译失败。

public static String[] sortNames(String[] info) {

    String[] firstName = new String[info.length];
    String[] lastName = new String[info.length];

    for(int i = 0; i < info.length; i++) {
        String[] infos = info[i].split("\t");
        firstName[i] = infos[0];
        lastName[i] = infos[1];
    }

    return firstName;//also,you might need to change your return type to String[][] so that both array can be returned
}

You could have your sortNames method return a two-dimensional array: 您可以让您的sortNames方法返回一个二维数组:

import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};
    String[][] names = sortNames(myArr);
    String[] firstNames = names[0];
    String[] lastNames = names[1];
    System.out.println("names: " + Arrays.deepToString(names));
    System.out.println("firstNames: " + Arrays.toString(firstNames));
    System.out.println("lastNames: " + Arrays.toString(lastNames));
  }

  public static String[][] sortNames(String[] info) {
    int infoArrLength = info.length;
    String[][] names = new String[2][infoArrLength];
    for(int i = 0; i < infoArrLength; i++) {
      String[] infos = info[i].split("\\s+");
      names[0][i] = infos[0];
      names[1][i] = infos[1];
    }
    return names;
  } 
}

Output: 输出:

names: [[Bob, Barbara, John], [Marley, Newton, Smith]]
firstNames: [Bob, Barbara, John]
lastNames: [Marley, Newton, Smith]
String[] myArr = {"Bob    Marley", "Barbara    Newton", "John    Smith"};
    String[] firstName = new String[myArr.length];
    String[] lastName = new String[myArr.length];
    for (int i = 0; i < myArr.length; i++) {
        String[] splitString = myArr[i].split("\\s+");
        firstName[i] = splitString[0];
        lastName[i] = splitString[1];
    }

You can simply use java regx to perform the splitting . 您可以简单地使用java regx执行splitting

\\s => A whitespace character, short for [ \\t\\n\\x0b\\r\\f] \\ s =>空格字符,是[\\ t \\ n \\ x0b \\ r \\ f]的缩写

private static void splitArray(String[] arr) {
    int len = arr.length;
    String[] firstNames = new String[len];
    String[] lastNames = new String[len];
    for (int i = 0; i < len; ++i) {
        String[] names = arr[i].split("\\s+");
        firstNames[i] = names[0];
        lastNames[i] = names[1];
    }
    System.out.println(Arrays.deepToString(firstNames));
    System.out.println(Arrays.deepToString(lastNames));
}

If I were you, in your case, I'd prefer using javafx.util.Pair to bind the first and last name together as follows and in java 8 using stream will make it cleaner: 如果您是我, javafx.util.Pair喜欢使用javafx.util.Pair如下将名字和姓氏绑定在一起,在java 8中使用stream可以使它更整洁:

private static void splitArrayUsingStream(String[] arr) {
    Pair[] pairs = Arrays.stream(arr).map(s -> {
        String[] names = s.split("\\s+");
        return new Pair<>(names[0], names[1]);
    }).collect(Collectors.toList()).toArray(new Pair[arr.length]);
    System.out.println(Arrays.deepToString(pairs));
}

These methods will give us output as: 这些方法将为我们提供以下输出:

[Bob, Barbara, John]
[Marley, Newton, Smith]
[Bob=Marley, Barbara=Newton, John=Smith]

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

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