In Java, convert a string array to a jagged array

I have a 1D array of numbers and need help splitting them into groups using a jagged array so that I can perform a series of computations. e.g.

int array []={1 2 3 4 5 6 7 8 9 10} 

I want to segregate and allocate the first four to one array, the next three to another, and the final three to a third. In my code, I was expecting to obtain something like this.

int [][] x = new int [3][];

x[0] = new int [4];
x[0] = {1 2 3 4};

x[1] = new int [3];
x[1] = {5 6 7};

x[2] = new int [3];
x[2] = {8 9 10};

Is there any method to create this jagged array using a flexible for loop and divide it into M groups or N groups of N values each just like an example given here that I didn’t understand? I attempted to retrieve those numbers using substring(), but I’m not sure how. I attempted to get those numbers using substring(), but I’m not sure how to proceed or if I’m doing it correctly.

for( int i=0; i<x.length; i++) { 
 x [i]= array.substring (0,3);
 x [i]=array.substring (4,6);
 x [i]=array.substring(7,9);
 }

I’m new to programming, but this code is plainly incorrect; could you kindly assist me? Thank you very much.

1 Like