【问题标题】:How do I do the product of a matrix of string in Java?如何在Java中做字符串矩阵的乘积?
【发布时间】:2018-03-02 06:26:15
【问题描述】:

我有一个未知数量的 String[]。每个数组可以有不同的大小。

我想为每个数组中的每个值创建“产品”(或连接)。

示例

public static void main(String[] args) {
    String[] x = {"a", "b", "c"};
    String[] y = {"d", "e", "f", "g"};
    String[] z = {"h", "i"};
    ...
}

所需的输出

输出将是adh, adi, aeh, aei, ...

我认为我必须通过递归来处理这个问题,因为我不知道我有多少个数组。但即便如此,我还是很难找到将结果存储在哪里。

任何指针?

【问题讨论】:

  • 这些字符串数组存储在哪里?程序是否接收到一个 String[] 数组?
  • 收到未知数​​量的列表或集合

标签: java string matrix concatenation cartesian-product


【解决方案1】:

好的,我找到路了

package eu.webfarmr;

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

public class Dummy {
    public static void main(String[] args) {
        String[] x = {"a", "b", "c"};
        String[] y = {"d", "e", "f", "g"};
        String[] z = {"h", "i"};
        ArrayList<String[]> list = new ArrayList<String[]>();
        list.add(x);
        list.add(y);
        list.add(z);
        List<String> result = product(list);
        for (String r : result){
            System.out.println(r);
        }
    }

    private static ArrayList<String> product(ArrayList<String[]> items){
        ArrayList<String> result = new ArrayList();
        if (items!=null && items.size()>0){
            String[] currentItem = items.get(0);
            ArrayList<String[]> clone = (ArrayList<String[]>) items.clone();
            clone.remove(0);
            for (String item : currentItem){                
                ArrayList<String> product = product(clone);
                if (product!=null && product.size()>0){
                    for (String p : product){
                        result.add(item+p);
                    }
                } else {
                    result.add(item);
                }
            }
        }
        return result;
    }
}

这段代码会输出

adh
adi
aeh
aei
afh
afi
agh
agi
bdh
bdi
beh
bei
bfh
bfi
bgh
bgi
cdh
cdi
ceh
cei
cfh
cfi
cgh
cgi

【讨论】:

    【解决方案2】:

    我做到了,而且效果很好。

    private static List<String> multiply(List<String> x, List<String> y) {
        List<String> results = new ArrayList<>();
        x.forEach(s1 -> y.forEach(s2 ->
                        results.add(s1+s2 )
                )
        );
        return results;
    }
    
    public static void main(String[] args) {
        String[] x = {"a", "b", "c"};
        String[] y = {"d", "e", "f", "g"};
        String[] z = {"h", "i"};
        List<List<String>> inputs = Arrays.asList(Arrays.asList(x),Arrays.asList(y),Arrays.asList(z));
        List<String> results = Arrays.asList("");
    
        for (int i = 0; i < inputs.size(); i++) {
            results = multiply(results, inputs.get(i));
        }
        System.out.println("results = " + results);
    

    }

    输出:

    adh adi aeh aei afh afi agh agi bdh bdi beh bei bfh bfi bgh bgi cdh cdi ceh cei cfh cfi cgh cgi

    【讨论】:

    • 它没有:您假设只有 3 个数组。问题是关于一个未知号码
    猜你喜欢
    • 2017-11-15
    • 2023-04-08
    • 2018-11-22
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    相关资源
    最近更新 更多