1

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]]
0

4 Answers 4

2

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];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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]]

Comments

0

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();
        }

    }
}

Comments

0

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]]

See also: Copying a 1d array to a 2d array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.