需要在Java中初始化四维数组的帮助吗

需要在Java中初始化四维数组的帮助吗,java,arrays,Java,Arrays,我有这个 buttons[0].setBounds(12, 161, 96, 25); // Numerical 0 buttons[1].setBounds(12, 50, 42, 25); // Numerical 1 buttons[2].setBounds(66, 50, 42, 25); // Numerical 2 buttons[3].setBounds(120, 50, 42, 25); // Numer

我有这个

        buttons[0].setBounds(12, 161, 96, 25);  // Numerical 0
        buttons[1].setBounds(12, 50, 42, 25);   // Numerical 1
        buttons[2].setBounds(66, 50, 42, 25);   // Numerical 2
        buttons[3].setBounds(120, 50, 42, 25);  // Numerical 3
        buttons[4].setBounds(12, 87, 42, 25);   // Numerical 4
        buttons[5].setBounds(66, 87, 42, 25);   // Numerical 5
        buttons[6].setBounds(120, 87, 42, 25);  // Numerical 6
        buttons[7].setBounds(12, 124, 42, 25);  // Numerical 7
        buttons[8].setBounds(66, 124, 42, 25);  // Numerical 8
        buttons[9].setBounds(120, 124, 42, 25); // Numerical 9
        buttons[10].setBounds(174, 124, 50, 62);// Equals button (=)
        buttons[11].setBounds(174, 50, 110, 25);// DEL Button (DEL)
        buttons[12].setBounds(120, 161, 42, 25);// DOT Button  (.)
        buttons[13].setBounds(174, 87, 50, 25); // MINUS Button(-)
        buttons[14].setBounds(234, 87, 50, 25); // PLUS Button (+)
        buttons[15].setBounds(236, 124, 50, 25);// PUTA Button (*)
        buttons[16].setBounds(236, 161, 50, 25);// DJEL Button (/)
我想优化它。这就是我如何使用数组和for循环为其他对象添加项的方法

JButton [] buttons = new JButton [17];
        String [] btnCommands = {"0","1","2","3","4","5","6","7","8","9","=","DEL",".","-","+","*","/"};

        for (int i = 0;i < btnCommands.length;i++) {
            buttons[i] = new JButton(btnCommands[i]);
        }

如何初始化和使用它?

我认为您不需要四维数组。你可能需要一个二维数组,我们是第二维度,其中有4个条目,每个条目对应一个边界

所以你会:

int [][] bounds = {{12, 161, 96, 25}, {12, 50, 42, 25}, ... }
你会像这样使用它:

buttons[0].setBounds(bounds[0][0], bounds[0][1], bounds[0][2], bounds[0][3]);

在循环中,只需将第一个维度替换为
i
,并保持对其他维度的访问权不变。

我将使用
JButton.setBounds(矩形)
方法,并具有一个
矩形数组

String[] btnCommands = {"0","1","2","3","4","5","6","7","8","9","=","DEL",".","-","+","*","/"};
Rectangle[] bounds = {
    new Rectangle(12, 161, 96, 25),  // Numerical 0
    new Rectangle(12, 50, 42, 25),   // Numerical 1
    new Rectangle(66, 50, 42, 25),   // Numerical 2
    new Rectangle(120, 50, 42, 25),  // Numerical 3
    new Rectangle(12, 87, 42, 25),   // Numerical 4
    new Rectangle(66, 87, 42, 25),   // Numerical 5
    new Rectangle(120, 87, 42, 25),  // Numerical 6
    new Rectangle(12, 124, 42, 25),  // Numerical 7
    new Rectangle(66, 124, 42, 25),  // Numerical 8
    new Rectangle(120, 124, 42, 25), // Numerical 9
    new Rectangle(174, 124, 50, 62),// Equals button (=)
    new Rectangle(174, 50, 110, 25),// DEL Button (DEL)
    new Rectangle(120, 161, 42, 25),// DOT Button  (.)
    new Rectangle(174, 87, 50, 25), // MINUS Button(-)
    new Rectangle(234, 87, 50, 25), // PLUS Button (+)
    new Rectangle(236, 124, 50, 25),// PUTA Button (*)
    new Rectangle(236, 161, 50, 25),// DJEL Button (/)
};

JButton[] buttons = new JButton[17];
for (int i = 0;i < btnCommands.length;i++) {
    JButton b = new JButton(btnCommands[i]);
    b.setBounds(bounds[i]);
    buttons[i] = b;
}
String[]btnCommands={“0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“=”、“DEL”、“+”、“-”、“+”、“*”、“/”};
矩形[]边界={
新矩形(12161,96,25),//数字0
新矩形(12,50,42,25),//数字1
新矩形(66,50,42,25),//数字2
新矩形(120,50,42,25),//数字3
新矩形(12,87,42,25),//数字4
新矩形(66,87,42,25),//数字5
新矩形(120,87,42,25),//数字6
新矩形(12,124,42,25),//数字7
新矩形(66、124、42、25),//数字8
新矩形(120124,42,25),//数字9
新矩形(174124 50 62),//等于按钮(=)
新矩形(174,50,110,25),//删除按钮(DEL)
新矩形(1201614225),//点按钮(.)
新矩形(174,87,50,25),//减号按钮(-)
新矩形(234,87,50,25),//加号按钮(+)
新矩形(236、124、50、25),//PUTA按钮(*)
新矩形(23616150 25),//DJEL按钮(/)
};
JButton[]按钮=新JButton[17];
对于(int i=0;i
1:您不需要四维数组;只要一个二维数组就足够了。2:省去了创建数组来存储这些值的麻烦,改用布局管理器。我认为你不需要四维数组。你可能需要一个二维数组,我们是第二维,其中有4个条目,每个边界一个。我不认为四维数组意味着你认为它意味着什么。如果你[希望不]使用四维数组。假设你把美国分成从0-n开始编号的地区,在这些地区,你将州编号为0-m,这些州的县编号也为0-o,其中有一些你关心的数据,索引为0-p。你可以做arr[地区][州][县][数据索引]。那肯定不是你想做的,是的!谢谢
String[] btnCommands = {"0","1","2","3","4","5","6","7","8","9","=","DEL",".","-","+","*","/"};
Rectangle[] bounds = {
    new Rectangle(12, 161, 96, 25),  // Numerical 0
    new Rectangle(12, 50, 42, 25),   // Numerical 1
    new Rectangle(66, 50, 42, 25),   // Numerical 2
    new Rectangle(120, 50, 42, 25),  // Numerical 3
    new Rectangle(12, 87, 42, 25),   // Numerical 4
    new Rectangle(66, 87, 42, 25),   // Numerical 5
    new Rectangle(120, 87, 42, 25),  // Numerical 6
    new Rectangle(12, 124, 42, 25),  // Numerical 7
    new Rectangle(66, 124, 42, 25),  // Numerical 8
    new Rectangle(120, 124, 42, 25), // Numerical 9
    new Rectangle(174, 124, 50, 62),// Equals button (=)
    new Rectangle(174, 50, 110, 25),// DEL Button (DEL)
    new Rectangle(120, 161, 42, 25),// DOT Button  (.)
    new Rectangle(174, 87, 50, 25), // MINUS Button(-)
    new Rectangle(234, 87, 50, 25), // PLUS Button (+)
    new Rectangle(236, 124, 50, 25),// PUTA Button (*)
    new Rectangle(236, 161, 50, 25),// DJEL Button (/)
};

JButton[] buttons = new JButton[17];
for (int i = 0;i < btnCommands.length;i++) {
    JButton b = new JButton(btnCommands[i]);
    b.setBounds(bounds[i]);
    buttons[i] = b;
}