0

I have this 2D array [[1, 0, 0], [2, 0, 0], [3, 4, 5,], [6, 7, 0], null, [0, 0, 0]].

I want to turn it into a dynamic array, meaning I want it to be [[1], [2], [3, 4, 5], [6, 7], null, []].

Below is my code for trying it out. I can't seem to get it. Any suggestions?

public static int[][] getNonFixedArray(String string) {

    String[] split = string.split("\\|", -1);
    int[][] result = new int[split.length][3];

    for (int i = 0; i < split.length; i++) {
        int[] array = getNonFixedArray(split[i]);

        if (array != null) {

            for (int j = 0; j < array.length; j++) {
                result[i][j] = array[j];

                if (result[i][j] == 0) {
                    result[i][j] = Integer.parseInt(" ");
                }
            }
        } else {
            result[i] = null;
        }
    }

    return result;
}
afsantos
  • 5,178
  • 4
  • 30
  • 54
InfoSS
  • 1
  • 2

1 Answers1

0

When you go through you inner arrays (the second for loop), you are trying to replace the element with value '0' with Integer.parseInt(" ");. This will probably give you an exception anyway. What might help you is to collect the non-zero numbers (in a list if you can use list) and create an array based on that after the second for loop ends.

Andy
  • 1,023
  • 1
  • 9
  • 17