[英]How to Fill a 2d array with a 1d array?
I'm seeking to place a 1 dimensional array inside of a 2d array with java. 我正在尝试用java在2d数组中放置一维数组。 With the method I call I'm passing the list that I'll be collecting the data from and also the 2d array I want to pass the information to.
使用我调用的方法,我将传递我将从中收集数据的列表以及我想要传递信息的2d数组。 After creating a set of an string I dissect the sentence by 2 place the characters into shingleSet.
在创建了一组字符串之后,我将句子解剖为2,将字符放入shingleSet中。 From shingleSet I must convert the strings to an array, sort the data then save the sorted array into the 2d array.
从shingleSet我必须将字符串转换为数组,对数据进行排序,然后将排序后的数组保存到2d数组中。 The problem I'm having is that I can load the array but I can only load it locally.
我遇到的问题是我可以加载数组,但我只能在本地加载它。 Would I have to return a 2d array from the method?
我是否必须从方法中返回一个二维数组?
private static void sentenceToShingles(int n, String[][] stringArray, List list){
int i, j;
String e;
String [] newShingle;
int counter = 0;
stringArray = new String [n][];
for(Object o: list){
//for every sentence create a new hashset
Set<String> shingleSet = new HashSet();
e = (String) o;
i=0;
while((i+2) < e.length()){
for(j=0;j<2;j++){
shingleSet.add(e.substring(i, i+2));
}
i++;
}
//add to array and sort ?RETURNING?
newShingle = shingleSet.toArray(new String[shingleSet.size()]);
Arrays.sort(newShingle);
//?? How can I save newShingle into 2d array??
//print shingle sorted shingle array
for(j=0; j<newShingle.length; j++){
System.out.print(newShingle[j] + " ");
}
System.out.println();
}
System.out.println();
}
To return the array from the method change the method signature from private static void
to private static String[][]
The add a return statement at the end of your code: return stringArray;
要从方法返回数组,请将方法签名从
private static void
更改为private static String[][]
在代码末尾添加一个return语句: return stringArray;
Update 更新
To fill a 2D array you need to loop over each row and column: 要填充2D数组,您需要遍历每一行和每列:
for(int row = 0; row < targetArray.length; row++) {
for(int col = 0; col < targetArray[row].length; col++) {
targetArray[row][col] = sourceArray[row];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.