将元素添加到一个多维数组 [英] Adding elements to a multidimensional Array
本文介绍了将元素添加到一个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我有一个ArrayList(mapArray),使得ArrayList中的每个元素包含一个字符串,如:
I currently have an ArrayList (mapArray) such that each element of the ArrayList contains a String such as:
################
因此,ArrayList的可以看看这样:
So the ArrayList could look as such:
################
#..............#
#........G.....#
#..............#
################
因此,每个元素是:
So each element is:
元素0:
################
元素1:
#..............#
元素2:
#........G.....#
元素3:
#..............#
元素4:
################
如何将我去这数据添加到一个多维阵列,使得第一元件将是行数和第二元件将是字符例如2DArray [2] [9] = G
How would i go about adding this data to a multidimensional array so that the first element would be the row number and the second element would be the character e.g. 2DArray[2][9] = G
到目前为止,我有
for(int i = 0; i < length; i++){
String element = mapArray.get(i);
elementSplit = element.split("(?!^)");
for(int j = 0; j < elementSplit.length; j++){
TwoDArray [i][j] = [i][elementSplit[j]];
}
}
这似乎并没有工作。
感谢任何帮助:)
而对于长期职位对不起。
And sorry for the long post.
推荐答案
此方法转换一个String的ArrayList到char [] []。希望这有助于。
This method converts a String ArrayList to char[][]. Hope this helps.
public static char[][] to2DCharArray(ArrayList<String> mapArray) {
char[][] c = new char[mapArray.size()][mapArray.get(0).length()];
for(int i = 0; i < mapArray.size(); i++) {
for(int j = 0; j < mapArray.get(i).length(); j++) {
c[i][j] = mapArray.get(i).charAt(j);
}
}
return c;
}
这篇关于将元素添加到一个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文