从另一个数组的每个第5个元素创建一个2D数组

时间:2021-01-14 21:40:39

Alright I have hit a brick wall and it has been killing me for two days and I am out of ideas. Basically what I have is a program that receives data back from a server using the companies API. The data comes back fine and I am able to turn it into an array without an issue. However, what I need is a secondary array created from values in this array. Let me show you:

好吧,我撞到了一堵砖墙,它已经杀了我两天了,我没有想法。基本上我所拥有的是一个使用公司API从服务器接收数据的程序。数据恢复正常,我可以把它变成一个没有问题的数组。但是,我需要的是根据此数组中的值创建的辅助数组。让我演示给你看:

Data Recieved and Parsed into Array:
String[] tag data = {d1,d2,d3,d4,d5,d6,d7,d8,d9,d10}  <-----these are populated automatically by the program. 

What I would need is another array created by say d1-d5 and then d6-d10, I have tried for loops and whatnot but the problem is it only prints the first five repeatedly.

我需要的是另一个由d1-d5然后d6-d10创建的数组,我试过循环等等但问题是它只重复打印前五个。

Here is the code I have so far:

这是我到目前为止的代码:

String[][] tags = null;

try {
    //Data is a string var that is passed to this method.It is the return data from the URL. 
    data = data.substring(61, data.length());
    String[] tagname = data.split(";");
    String[] secondArray = new String[5];
    for(int x = 0; x <= tagname.length; x++) {
        for(int i = 0; i <= 5; i++) {
            secondArray[i] = tagname[x];
        }
        tags[x] = secondArray;
    }
    Data.setTagArray(tags);
} catch(Exception e) {
    e.printStackTrace();
}

This is the data I get back:

这是我收到的数据:

["Lamp_Status", null, null, null, null]
["Lamp_Status", 1, null, null, null]
["Lamp_Status", 1, 0, null, null]
["Lamp_Status", 1, 0, 0, null]
["Lamp_Status", 1, 0, 0, 654722]

I don't need a specific answer I just need help getting in the right direction. I am not sure what is going on here or how I can make this work. Once again to recap I need to create an array of 1-5, 6-10 elements of another array.

我不需要特定的答案,我只需要帮助我们朝着正确的方向前进。我不确定这里发生了什么,或者我怎么能做到这一点。再次回顾一下,我需要创建另一个数组的1-5,6-10个元素的数组。

2 个解决方案

#1


Can you try

你能试一下吗

String[][] secondArray = new String[(tagname.length)/5][5];
for(int x = 0; x<=(tagname.length)/5; x++){
    for(int i = 0; i <= 5; i++)
        secondArray[x][i] = tagname[x]; 
}

#2


String[][] tags = null;

try {
    // Data is a string var that is passed to this method.It is the
    // return data from the URL.
    tags = new String[2][5];
    String[] tagname = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10"};
    String[] secondArray = new String[5];

    tags[0] = Arrays.copyOfRange(tagname, 0, 5);
    tags[1] = Arrays.copyOfRange(tagname, 5, 10);
    System.out.println(Arrays.toString(tags[0]));
    System.out.println(Arrays.toString(tags[1]));
} catch(Exception e) {
    e.printStackTrace();
}

Or copy the ranges you need.

或者复制您需要的范围。

#1


Can you try

你能试一下吗

String[][] secondArray = new String[(tagname.length)/5][5];
for(int x = 0; x<=(tagname.length)/5; x++){
    for(int i = 0; i <= 5; i++)
        secondArray[x][i] = tagname[x]; 
}

#2


String[][] tags = null;

try {
    // Data is a string var that is passed to this method.It is the
    // return data from the URL.
    tags = new String[2][5];
    String[] tagname = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "d10"};
    String[] secondArray = new String[5];

    tags[0] = Arrays.copyOfRange(tagname, 0, 5);
    tags[1] = Arrays.copyOfRange(tagname, 5, 10);
    System.out.println(Arrays.toString(tags[0]));
    System.out.println(Arrays.toString(tags[1]));
} catch(Exception e) {
    e.printStackTrace();
}

Or copy the ranges you need.

或者复制您需要的范围。