在一些特殊条件下操作二维字符串数组

时间:2017-05-25 10:14:43

标签: java arrays

我正在处理通过csv文件传来的大量数据,并将特定列(比如最后一列)中的二维数组作为多个值进行处理。需要使用除多值列之外的值复制同一行,该列应按如下方式分隔。 (行的顺序无关紧要)

Row 1 -->{hi, good, morning}
Row 2 -->{hi, good, afternoon}

示例输入(2D字符串数组):

String[][] masterArray = {
                {"hi", "good", "morning,afternoon"},
                {"have a", "nice", "day,trip,journey"},
                {"hi", "good", "evening"},
                {"I", "love", "you,India"},
                {"enjoy", "your", "dinner"}
        };

预期输出(二维字符串数组):

            String[][] masterOutput = {
                    {"hi", "good", "morning"},
                    {"hi", "good", "afternoon"},
                    {"have a", "nice", "day"},
                    {"have a", "nice", "trip"},
                    {"have a", "nice", "journey"},
                    {"hi", "good", "evening"},
                    {"I", "love", "you"},
                    {"I", "love", "India"},
                    {"Enjoy", "your", "dinner"},
        };

4 个答案:

答案 0 :(得分:0)

假设您确切知道应该拥有多少列。这可以通过双循环来完成。迭代该循环内的每一行,为多值列中的每个字符串创建一个新数组。将此添加到arraylist。在末尾。将arraylist转换为数组。

答案 1 :(得分:0)

这是使用Java 8流的“一线”方法:

Arrays.stream(masterArray)
        .flatMap(row -> Arrays.stream(row[row.length - 1].split(","))
                    .map(newRowTerminator -> {
                        final String[] fullNewRow = row.clone();
                        fullNewRow[row.length - 1] = newRowTerminator;
                        return fullNewRow;
                    })
        )
        .toArray(String[][]::new);

答案 2 :(得分:0)

这是你的解决方案:

import java.util.ArrayList;
import java.util.List;

public class MyClass {

    public static String[][] expandColumn(String[][] masterArray) {
        List<String[]> masterOutputList = new ArrayList<>();
        for (int i = 0; i < masterArray.length; i++) {
            String itemToExpand = masterArray[i][masterArray[i].length - 1];
            String[] parts = itemToExpand.split(",");
            for (int j = 0; j < parts.length; j++) {
                String[] row = new String[masterArray[i].length];
                System.arraycopy(masterArray[i], 0, row, 0, masterArray[i].length - 1);
                row[row.length - 1] = parts[j];
                masterOutputList.add(row);
            }
        }
        return masterOutputList.toArray(new String[0][0]);
    }

    public static void main(String[] args) {
        String[][] masterArray = {
                {"hi", "good", "morning,afternoon"},
                {"have a", "nice", "day,trip,journey"},
                {"hi", "good", "evening"},
                {"I", "love", "you,India"},
                {"enjoy", "your", "dinner"}
        };
        String[][] ss = expandColumn(masterArray);
        for (int i = 0; i < ss.length; i++) {
            for (int j = 0; j < ss[i].length; j++) {
                System.out.print(ss[i][j]);
            }
            System.out.println();
        }
    }

}

答案 3 :(得分:0)

我已经发布了answer using streams,但这是一个较低级别的方法,可能会或可能不会更高效 - 我现在没有时间运行基准测试。

int newRowsRequired = 0;

for (final String[] row : masterArray) {
    final String rowTerminators = row[row.length - 1];
    newRowsRequired += rowTerminators.length() - rowTerminators.replace(",", "").length() + 1;
}

final String[][] outArray = new String[newRowsRequired][];

int currentRow = 0;

for (final String[] row : masterArray) {
    for (final String rowTerminator : row[row.length - 1].split(",")) {
        final String[] newRow = row.clone();
        newRow[row.length - 1] = rowTerminator;
        outArray[currentRow++] = newRow;
    }
}