如何嵌套for循环以打印多维数组?

时间:2013-05-02 19:36:59

标签: java arrays nested

在我的程序中,我创建了一个现在已经从int转换为多维数组字符串employeeNamesA[2][i]的数组,我现在想要使用字符串构建器,以便我可以在一个JOptionPane中打印这个。 showMessageDialog。我试图让我的嵌套循环工作时遇到问题。

StringBuilder builder = new StringBuilder (employeeNamesA.length);
    for (int i=0; i<employeeNamesA.length; builder.append(employeeNamesA[][i++]))
        {
        for(int j=0; j<2; builder.append(employeeNamesA[j++][]))
        builder.append("\n");
        }
    JOptionPane.showMessageDialog(null, "Employees with new Salary: " + builder.toString() + "PBI will now be paying this much more in total salary: $" + (totalSalary - totalNewSalary));

1 个答案:

答案 0 :(得分:1)

你在找这样的东西吗?

StringBuilder builder = new StringBuilder (employeeNamesA.length);
    for (int i=0; i<employeeNamesA.length; i++))
        {
        for(int j=0; j<2; j++)
            builder.append(employeeNamesA[i][j]+"\n");
        }
    JOptionPane.showMessageDialog(null, "Employees with new Salary: " + builder.toString() + "PBI will now be paying this much more in total salary: $" + (totalSalary - totalNewSalary));