将一个一维数组转换为二维数组

huangapple go评论90阅读模式
英文:

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(&quot;&quot;);
[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 &lt; arr.length; i++) {
    for (int j = 0; j &lt; 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 &lt;arr.length ; i++) {
    for (int j = 0; j &lt;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 &lt; array.length; i++)
	arr[i / 3][i % 3] = array[i];

Test

String song = &quot;WUBWUBABCWUB&quot;;
String[] array = song.split(&quot;&quot;);
System.out.println(Arrays.toString(array));

String[][] arr = new String[4][3];
for (int i = 0; i &lt; 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 = { &quot;W&quot;, &quot;U&quot;, &quot;B&quot;, &quot;W&quot;, &quot;U&quot;, &quot;B&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;W&quot;, &quot;U&quot;, &quot;B&quot; };
String array2[][] = new String[4][3];
// One dimension array into a two dimension array.
int k = 0;
for (int i = 0; i  &lt;array2.length ; i++)
{
for (int j = 0; j &lt; array2[j].length; j++)
{
array2[i][j] = array[k++];
}
}
// Print 1 dimension array.
System.out.println(&quot;array:&quot;);
for (int i = 0; i &lt; array.length; i++)
{
System.out.print(array[i] + &quot; &quot;);
}
// Print 2 dimension array.
System.out.println(&quot;\n\narray2:&quot;);
for (int i = 0; i &lt; array2.length; i++)
{
for (int j = 0; j &lt; array2[i].length; j++)
{
System.out.print(array2[i][j] + &quot; &quot;);
}
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 = &quot;WUBWUBABCWUB&quot;.split(&quot;&quot;);

String[][] arr2d = IntStream
        .iterate(0, i -&gt; i &lt; arr.length, i -&gt; i + 3)
        .mapToObj(i -&gt; 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>

huangapple
  • 本文由 发表于 2020年4月10日 07:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61131943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定