繁体   English   中英

如何将一维数组与二维数组结合起来

[英]How to combine a one-dimensional array with a two-dimensional array

我有一系列客户姓名:

String[] customers = {"Joe Dirt", "John Dirt", "Josh Dirt"};

我还有一个包含账户号码/付款的二维数组:

double[][] payments = {{1011,12,54,39},{1012,55,65,48},{1013,56,92,55}};

我需要将客户的姓名与他们的帐户和付款进行匹配并打印出来。 arrays 已经排序,我只需要打印出“Joe Dirt 1011、12、54、55”等等。我试图了解如何循环遍历二维数组。

这就是我所拥有的:

public static ArrayList<String> combineArray(String[] customers,
                                             double[][] payments) {

    ArrayList<String> customerPayments = new ArrayList<String>();

    String result = "";
    String custName = "";
    String acctInfo = "";

    for (String n : customers) {
        custName = n;
        for (double[] x : payments) {
            for (double y : x) {
                acctInfo = String.valueOf(y);
            }
        }
        result = custName + acctInfo;
        customerPayments.add(result);
    }
    return customerPayments;
}

它正确打印出客户姓名,但仅打印出付款二维数组的最后一个值。 前任。

Joe Dirt55
John Dirt55
Josh Dirt55

我在这里俯瞰什么?

例如,您可以这样做:

String[] customers = {"Joe Dirt", "John Dirt", "Josh Dirt"};
double[][] payments = {{1011,12,54,39},{1012,55,65,48},{1013,56,92,55}};
for (int i = 0; i < customers.length; i ++) {
    System.out.println(customers[i] + Arrays.toString(payments[i]));
}

或者,如果您想查看不带括号的 output 字符串,请尝试这样做:

for (int i = 0; i < customers.length; i++) {
    System.out.println(customers[i] + " " +
            Arrays.toString(payments[i]).replaceAll("\\[|\\]", ""));
}

您可以采取多种方法。 首先,我建议你编程到List接口(而不是ArrayList具体类型)。 其次,在第一个数组中使用显式索引,以便您可以在payments数组中跟踪您的 position。 第三, StringJoiner可能对组合元素很有用。 就像是,

public static List<String> combineArray(String[] customers, double[][] payments) {
    List<String> customerPayments = new ArrayList<>();
    for (int i = 0; i < customers.length; i++) {
        StringJoiner sj = new StringJoiner(", ");
        for (double x : payments[i]) {
            sj.add(String.format("%.0f", x));
        }
        customerPayments.add(String.format("%s %s", customers[i], sj));
    }
    return customerPayments;
}

为了更容易验证

public static void main(String[] args) {
    String[] customers = { "Joe Dirt", "John Dirt", "Josh Dirt" };
    double[][] payments = {
            { 1011, 12, 54, 39 },
            { 1012, 55, 65, 48 },
            { 1013, 56, 92, 55 }
    };
    combineArray(customers, payments).stream().forEach(System.out::println);
}

我得到

Joe Dirt 1011, 12, 54, 39
John Dirt 1012, 55, 65, 48
Josh Dirt 1013, 56, 92, 55
  1. customers周期中使用索引,以便您可以使用它来获得相应的payments
  2. 您需要连接acctInfo ,而不是每次都替换它的值
  3. 您需要在customers周期开始时清除acctInfo ,这样它就不会累积以前客户的付款

例如:

public static ArrayList<String> combineArray(String[] customers, double[][] payments) {

    ArrayList<String> customerPayments = new ArrayList<>();

    for (int i = 0; i < customers.length; i++) {
        String acctInfo = "";
        String custName = customers[i];
        for (double y : payments[i]) {
            acctInfo += String.valueOf(y);
        }
        String result = custName + acctInfo;
        customerPayments.add(result);
    }
    return customerPayments;
}

暂无
暂无

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

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