好吧,我撞到了一块砖墙,它已经折磨了我两天了,我已经没有主意了。基本上,我拥有的是一个程序,它使用companies 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. 我需要的是另一个数组,比如d1-d5,然后是d6-d10,我尝试了循环之类的东西,但问题是它只重复打印前五个。
下面是我到目前为止掌握的代码:
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();
}这是我得到的数据:
["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]我不需要一个具体的答案,我只需要帮助我朝着正确的方向前进。我不知道这是怎么回事,也不知道我怎样才能把这件事办好。再次回顾一下,我需要创建一个由1-5,6-10个元素组成的数组。
发布于 2015-05-11 13:16:37
你能试试吗
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];
}发布于 2015-05-11 13:28:34
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();
}或者复制你需要的范围。
https://stackoverflow.com/questions/30168533
复制相似问题