且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在Java中的二维数组中动态生成项目

更新时间:2022-06-11 01:58:06

如果我正确理解了您的问题,则需要执行以下步骤:

If i understand your question correctly, you need this steps :

创建数字数组:

String[] numbers = {"One", "Two", "Three", ...};

在该数组上循环并检查索引是否为偶数,使用 Boolean.TRUE 否则使用 Boolean.FALSE

Loop over this array and check if the index in even or not it yes use Boolean.TRUE else use Boolean.FALSE

String[] numbers = {"One", "Two", "Three", ...};
Object[][] data = new Object[numbers.length][];
for (int i = 0; i < numbers.length; i++) {
    if (i % 2 == 0) {
        data[i] = new Object[]{numbers[i], Boolean.TRUE};
    } else {
        data[i] = new Object[]{numbers[i], Boolean.FALSE};
    }
}

如果使用:

System.out.println(Arrays.deepToString(data));

输出为:

[[One, true], [Two, false], [Three, true], ...]






但这不是存储值的更好方法,而是可以创建自己的Object,该Object可以具有两个参数String和Boolean:


But this is not the better way you store values, instead you can create your own Object which can have two parameters String and Boolean :

class MyObject{
   String n;
   Boolean;
   ...
}