Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 - Fatal编程技术网

Java 如何使用另一列作为条件连接矩阵中某列的行

Java 如何使用另一列作为条件连接矩阵中某列的行,java,arrays,Java,Arrays,Supose a我有以下数组: String[ ] array = new String { { a2, a4, a5, a1, a3 }, {5.0, 2.0, 5.0, 2.0, 2.0 } }; 按5X2矩阵排列: (a2;2.0) (a4;2.0) (a5;2.0) (a1;5.0) (a3;5.0) 我希望使用第二列中的值作为标准连接第一列的行,生成以下2X2矩阵: (“a2、a4、a5;2.0) (“a1、a3;5.0) 谢谢 不知道您的数字是如何存储的,但

Supose a我有以下数组:

String[ ] array = new String {
    { a2,  a4,  a5,  a1,  a3 },
    {5.0, 2.0, 5.0, 2.0, 2.0 }
};
按5X2矩阵排列:

(a2;2.0)

(a4;2.0)

(a5;2.0)

(a1;5.0)

(a3;5.0)

我希望使用第二列中的值作为标准连接第一列的行,生成以下2X2矩阵:

(“a2、a4、a5;2.0)

(“a1、a3;5.0)


谢谢

不知道您的数字是如何存储的,但假设它们是大小数(出于哈希原因),请使用
Map
聚合项目。因此,如果您的映射已经
包含()
给定的十进制数,
append()
到相应的
StringBuilder
,如果没有,则创建一个新的。另外,对于迭代矩阵,使用带有共享索引的
for
循环并行迭代两个数组。

不知道您的数字是如何存储的,但假设它们是大小数(出于哈希原因),请使用
Map
聚合项。因此,如果您的映射已经
包含()
给定的十进制数,
append()
到相应的
StringBuilder
,如果没有,则创建一个新的。另外,对于迭代矩阵,使用带有共享索引的
for
循环并行迭代两个数组。

我认为这不是最好/最安全的代码(我正在对数组进行一些假设),但它会让您开始

首先,修复阵列:

String[][] array = new String[][]{{"a2","a4","a5","a1","a3"},
                                 {"5.0", "2.0", "5.0", "2.0", "2.0"}};
现在,

Map Map=newhashmap();
对于(int i=0;i

我想我可以用streams实现这一点,但我必须考虑一下。

我认为这不是最好/最安全的代码(我正在对数组进行一些假设),但它会让您开始

首先,修复阵列:

String[][] array = new String[][]{{"a2","a4","a5","a1","a3"},
                                 {"5.0", "2.0", "5.0", "2.0", "2.0"}};
现在,

Map Map=newhashmap();
对于(int i=0;i

我想我可以用streams实现这一点,但我必须考虑一下。

我们需要做的第一件事是将数组类型更改为
Object[][]
,这样它可以同时保存字符串和数字:

Object[][] array = {
    { "a2",  "a4",  "a5",  "a1",  "a3" },
    { 5.0, 2.0, 5.0, 2.0, 2.0 }
};
现在,我们使用流生成由相应数值分组的串联字符串值的映射,然后将映射项流到一个2列矩阵中:

Object[][] grouped = IntStream.range(0, array[0].length) // this will create a stream containing as many natural numbers, starting from 0, that the array has elements
        .boxed() // this will make it a stream of Integers instead of ints
        .collect(groupingBy(i -> array[1][i], mapping(i -> array[0][i].toString(), joining(", ")))) // here, we make a Map out of the stream by grouping by the i'th element of the second column in the matrix and taking as the value the respective element from the first column, then joining the resulting list of strings in the map's value with a ", " separator
        .entrySet() // now we take the entry set of the map
        .stream() // we stream it
        .map(e -> new Object[] {e.getValue(), e.getKey()}) // and we transform each entry in the map into a matrix row
        .toArray(Object[][]::new); // finally, we transform the stream of rows into a matrix (array of rows)
以上依赖于java.util.stream.Collectors.*
的静态导入


如果您所追求的是纯字符串矩阵,则可以很容易地对其进行调整。

我们需要做的第一件事是将数组类型更改为
Object[][]
,这样它就可以同时容纳字符串和数字:

Object[][] array = {
    { "a2",  "a4",  "a5",  "a1",  "a3" },
    { 5.0, 2.0, 5.0, 2.0, 2.0 }
};
现在,我们使用流生成由相应数值分组的串联字符串值的映射,然后将映射项流到一个2列矩阵中:

Object[][] grouped = IntStream.range(0, array[0].length) // this will create a stream containing as many natural numbers, starting from 0, that the array has elements
        .boxed() // this will make it a stream of Integers instead of ints
        .collect(groupingBy(i -> array[1][i], mapping(i -> array[0][i].toString(), joining(", ")))) // here, we make a Map out of the stream by grouping by the i'th element of the second column in the matrix and taking as the value the respective element from the first column, then joining the resulting list of strings in the map's value with a ", " separator
        .entrySet() // now we take the entry set of the map
        .stream() // we stream it
        .map(e -> new Object[] {e.getValue(), e.getKey()}) // and we transform each entry in the map into a matrix row
        .toArray(Object[][]::new); // finally, we transform the stream of rows into a matrix (array of rows)
以上依赖于java.util.stream.Collectors.*
的静态导入


如果您所追求的是纯字符串矩阵,那么可以很容易地对其进行调整。这就是我在不使用流的情况下解决问题的方法。然而,正如其他人所说,我不确定使用哪种数据类型

private static final int SIZE_KEY_PLUS_CONCATENATION = 2;
private static final int INDEX_OF_KEY = 1;
private static final int INDEX_OF_VALUE = 0;
private static final String SEPARATOR = ", ";

private static final int RESULT_KEY = 0;
private static final int RESULT_VALUE = 1;
private static final String RESULT_SEPARATOR = " : ";

public static void main(String[] args) {
    String[][] matrix = new String[][]{
            {"a2", "5.0"},
            {"a4", "2.0"},
            {"a5", "5.0"},
            {"a1", "2.0"},
            {"a3", "2.0"},
    };

    String[][] result = getConcatenatedMatrix(matrix);
    for(String[] row : result) {
        System.out.println(row[RESULT_KEY] + RESULT_SEPARATOR + row[RESULT_VALUE]);
    }
}

private static String[][] getConcatenatedMatrix(String[][] inputMatrix) {
    Map<String, String> map = generateMapWithConcatenatedValues(inputMatrix);
    return convertMapToMatrix(map);

}

private static Map<String, String> generateMapWithConcatenatedValues(String[][] inputMatrix) {
    Map<String, String> map = new HashMap<>();
    for(String[] row : inputMatrix) {
        String key = row[INDEX_OF_KEY];
        String value = row[INDEX_OF_VALUE];
        doConcatenation(key, value, map);
    }

    return map;
}

private static void doConcatenation(String key, String value, Map<String, String> map) {
    if(map.containsKey(key)) {
        map.put(key, map.get(key) + SEPARATOR + value);
    } else {
        map.put(key, value);
    }
}

private static String[][] convertMapToMatrix(Map<String, String> mapToConvert) {
    int keyCount = mapToConvert.size();
    String[][] result = new String[keyCount][SIZE_KEY_PLUS_CONCATENATION];
    Set<String> keys = mapToConvert.keySet();
    int currentKey = 0;
    for(String key : keys) {
        result[currentKey][RESULT_KEY] = key;
        result[currentKey][RESULT_VALUE] = mapToConvert.get(key);
        currentKey++;
    }

    return result;
}
private static final int SIZE\u KEY\u PLUS\u CONCATENATION=2;
_KEY=1的私有静态最终int索引_;
私有静态最终整数索引,其值为0;
专用静态最终字符串分隔符=“,”;
私有静态最终整数结果_KEY=0;
私有静态最终整数结果_值=1;
私有静态最终字符串结果_SEPARATOR=“:”;
公共静态void main(字符串[]args){
字符串[][]矩阵=新字符串[][]{
{“a2”,“5.0”},
{“a4”,“2.0”},
{“a5”,“5.0”},
{“a1”,“2.0”},
{“a3”,“2.0”},
};
字符串[][]结果=getConcatenatedMatrix(矩阵);
对于(字符串[]行:结果){
System.out.println(行[结果\键]+结果\分隔符+行[结果\值]);
}
}
私有静态字符串[][]getConcatenatedMatrix(字符串[][]inputMatrix){
Map Map=带有串联值的GenerateMap(输入矩阵);
返回convertMapToMatrix(map);
}
私有静态映射生成器带连接值(字符串[][]inputMatrix){
Map Map=newhashmap();
对于(字符串[]行:inputMatrix){
字符串键=行[键的索引];
字符串值=行[值的索引];
文档分类(键、值、地图);
}
返回图;
}
私有静态void doconcatation(字符串键、字符串值、映射){
if(地图容器(图例)){
map.put(key,map.get(key)+分隔符+值);
}否则{
map.put(键、值);
}
}
私有静态字符串[][]convertMapToMatrix(Map-mapToConvert){
int keyCount=mapToConvert.size();
字符串[][]结果=新字符串[keyCount][SIZE\u KEY\u PLUS\u串联];
Set keys=mapToConvert.keySet();
int currentKey=0;
用于(字符串键:键){
结果[currentKey][result\u KEY]=键;
结果[currentKey][result_VALUE]=mapToConvert.get(键);
currentKey++;
}
返回结果;
}

这就是我在不使用流的情况下解决问题的方法。然而,正如其他人所说,我不确定使用哪种数据类型

private static final int SIZE_KEY_PLUS_CONCATENATION = 2;
private static final int INDEX_OF_KEY = 1;
private static final int INDEX_OF_VALUE = 0;
private static final String SEPARATOR = ", ";

private static final int RESULT_KEY = 0;
private static final int RESULT_VALUE = 1;
private static final String RESULT_SEPARATOR = " : ";

public static void main(String[] args) {
    String[][] matrix = new String[][]{
            {"a2", "5.0"},
            {"a4", "2.0"},
            {"a5", "5.0"},
            {"a1", "2.0"},
            {"a3", "2.0"},
    };

    String[][] result = getConcatenatedMatrix(matrix);
    for(String[] row : result) {
        System.out.println(row[RESULT_KEY] + RESULT_SEPARATOR + row[RESULT_VALUE]);
    }
}

private static String[][] getConcatenatedMatrix(String[][] inputMatrix) {
    Map<String, String> map = generateMapWithConcatenatedValues(inputMatrix);
    return convertMapToMatrix(map);

}

private static Map<String, String> generateMapWithConcatenatedValues(String[][] inputMatrix) {
    Map<String, String> map = new HashMap<>();
    for(String[] row : inputMatrix) {
        String key = row[INDEX_OF_KEY];
        String value = row[INDEX_OF_VALUE];
        doConcatenation(key, value, map);
    }

    return map;
}

private static void doConcatenation(String key, String value, Map<String, String> map) {
    if(map.containsKey(key)) {
        map.put(key, map.get(key) + SEPARATOR + value);
    } else {
        map.put(key, value);
    }
}

private static String[][] convertMapToMatrix(Map<String, String> mapToConvert) {
    int keyCount = mapToConvert.size();
    String[][] result = new String[keyCount][SIZE_KEY_PLUS_CONCATENATION];
    Set<String> keys = mapToConvert.keySet();
    int currentKey = 0;
    for(String key : keys) {
        result[currentKey][RESULT_KEY] = key;
        result[currentKey][RESULT_VALUE] = mapToConvert.get(key);
        currentKey++;
    }

    return result;
}
private static final int SIZE\u KEY\u PLUS\u CONCATENATION=2;
_KEY=1的私有静态最终int索引_;
私有静态最终整数索引