英文:
Converting a 1D array to a 2D array
问题
得到一个数组:
String[] array = song.split("");
[W, U, B, W, U, B, A, B, C, W, U, B]
我需要将它转换为二维数组:
String[][] arr = new String[4][3];
[[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
以下代码:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[j].length; j++) {
arr[i][j] = array[j];
}
}
得到的结果为:
[[W, U, B], [W, U, B], [W, U, B], [W, U, B]]
如何得到:
[[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
英文:
Got an array:
String[] array = song.split("");
[W, U, B, W, U, B, A, B, C, W, U, B]
I need to convert it in two dimension:
String[][] arr = new String[4][3];
[[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
Below code:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[j].length; j++) {
arr[i][j] = array[j];
}
}
gives the result:
[[W, U, B], [W, U, B], [W, U, B], [W, U, B]]
How to get:
[[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
答案1
得分: 2
你正在将错误的值赋给 arr[i][j]
:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[j].length; j++) {
arr[i][j] = array[i * arr[j].length + j];
}
}
英文:
You're assigning the wrong value to arr[i][j]
:
for (int i = 0; i <arr.length ; i++) {
for (int j = 0; j <arr[j].length ; j++) {
arr[i][j] = array[i * arr[j].length + j];
}
}
答案2
得分: 0
你可以通过单个循环完成,迭代array
,并使用除法和取余运算符计算索引到arr
中。
for (int i = 0; i < array.length; i++)
arr[i / 3][i % 3] = array[i];
测试
String song = "WUBWUBABCWUB";
String[] array = song.split("");
System.out.println(Arrays.toString(array));
String[][] arr = new String[4][3];
for (int i = 0; i < array.length; i++)
arr[i / 3][i % 3] = array[i];
System.out.println(Arrays.deepToString(arr));
输出
[W, U, B, W, U, B, A, B, C, W, U, B]
[[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
英文:
You can do it with a single loop, iterating over array
, and calculating the indexes into arr
using division and remainder operators.
for (int i = 0; i < array.length; i++)
arr[i / 3][i % 3] = array[i];
Test
String song = "WUBWUBABCWUB";
String[] array = song.split("");
System.out.println(Arrays.toString(array));
String[][] arr = new String[4][3];
for (int i = 0; i < array.length; i++)
arr[i / 3][i % 3] = array[i];
System.out.println(Arrays.deepToString(arr));
Output
[W, U, B, W, U, B, A, B, C, W, U, B]
[[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
答案3
得分: 0
问题出在 arr[i][j] = array[j]
这一行。变量 j
应该继续使用。相反,应该使用一个变量,当外部循环进入下一次迭代时,不会再次赋值为 0
,因为内部循环会再次将 j
赋值为 0
。在循环外部使用另一个计数器来维护这个值,这个例子中使用 k
。
public class Main
{
public static void main(String[] args)
{
String[] array = { "W", "U", "B", "W", "U", "B", "A", "B", "C", "W", "U", "B" };
String array2[][] = new String[4][3];
// One dimension array into a two dimension array.
int k = 0;
for (int i = 0; i < array2.length; i++)
{
for (int j = 0; j < array2[i].length; j++)
{
array2[i][j] = array[k++];
}
}
// Print 1 dimension array.
System.out.println("array:");
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
// Print 2 dimension array.
System.out.println("\n\narray2:");
for (int i = 0; i < array2.length; i++)
{
for (int j = 0; j < array2[i].length; j++)
{
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
}
}
英文:
The problem is in arr[i][j] = array[j]
. The variable j
should continue. Instead use a variable that doesn't assign 0
again when the outer loop pass to the next iteration because the inner loop do the assing j = 0
again. Use another counter outside of the loops to maintain the value, in this case k
.
public class Main
{
public static void main(String[] args)
{
String[] array = { "W", "U", "B", "W", "U", "B", "A", "B", "C", "W", "U", "B" };
String array2[][] = new String[4][3];
// One dimension array into a two dimension array.
int k = 0;
for (int i = 0; i <array2.length ; i++)
{
for (int j = 0; j < array2[j].length; j++)
{
array2[i][j] = array[k++];
}
}
// Print 1 dimension array.
System.out.println("array:");
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
// Print 2 dimension array.
System.out.println("\n\narray2:");
for (int i = 0; i < array2.length; i++)
{
for (int j = 0; j < array2[i].length; j++)
{
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
}
}
答案4
得分: 0
你可以按照以下方式处理这个数组:
String[] arr = "WUBWUBABCWUB".split("");
String[][] arr2d = IntStream
.iterate(0, i -> i < arr.length, i -> i + 3)
.mapToObj(i -> Arrays
.stream(arr, i, Math.min(i + 3, arr.length))
.toArray(String[]::new))
.toArray(String[][]::new);
System.out.println(Arrays.deepToString(arr2d));
// [[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
另请参阅:将一维数组复制到二维数组
英文:
You can process this array as follows:
String[] arr = "WUBWUBABCWUB".split("");
String[][] arr2d = IntStream
.iterate(0, i -> i < arr.length, i -> i + 3)
.mapToObj(i -> Arrays
.stream(arr, i, Math.min(i + 3, arr.length))
.toArray(String[]::new))
.toArray(String[][]::new);
System.out.println(Arrays.deepToString(arr2d));
// [[W, U, B], [W, U, B], [A, B, C], [W, U, B]]
<sup>See also: Copying a 1d array to a 2d array</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论