sona11

sona11

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.

Most Liked

finner

finner

hi @sona11 - this is a very good problem for learning how to program.
There is no method in Java to do this, you need to write it. I want to help you but I am not going to give you the code, you will not learn that way.

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

Your example array is an array of int which is a primitive data type in Java, not an object like String, List, etc.

The substring() in Java is used with the String object which is why it will not work with an array or an int.
Examples:

        String s = "Jagged Array";

        s.substring(7);             // will return Array
        s.substring(0, 6);        // will return Jagged

The example you refer to can help.

Copy that example to your IDE and change the following lines:

Replace this;

Scanner scn = new Scanner(System.in);
      
//Taking the input from user
int r = scn.nextInt();

with this:

int r = 3;  

We are just removing the user input from the code and hardcoding the number of rows, in this case we put 3.

Then run it in debug. You can also change the value of r each time you run to see what happens. This should help you understand what it is doing.

Pay special attention to the nested loop when you are stepping in debug.

        int temp = 0;
        for (int i = 0; i < arr.length; i++)
            for (int j = 0; j < arr[i].length; j++)
                arr[i][j] = temp++;

Once you understand that example you can start thinking about how to make it more flexible. I will try to help you with that if you want (but I won’t give you code).

What version of Java are you using? And what IDE, if you are using an IDE?
I hope this helps you in the learning process.

Maartz

Maartz

Hi,

Well to achieve some sort of output,
I’d use a regex with a capture. By explicitly saying I want a 4/3/3 capture.
The Regex should look like this:

[0-9]{4}\b-[0-9]{3}\b-[0-9]{3}

Note that this regex expects a ‘-’ as separator. Making use whitespace should do the trick.

What do you think about it?

Where Next?

Popular Frontend topics Top

dyowee
Why or when should one choose Tailwind over Bootstrap? :slight_smile:
New
brian
I’m working with a designer who created a design in Photoshop. I am mostly a Node.js and Android dev, but when I have worked with designe...
New
axelson
In Elixir I love to use the library GitHub - sasa1977/boundary: Manage and restrain cross-module dependencies in Elixir projects to enfor...
New
pillaiindu
I mean, when you render all the HTML at the server side and the data is sent through HTTP requests, except only if some tiny things are d...
New
beberardinelli
Hi! I just started coding a few months ago and I am trying to get all the help I can get. My friend showed me this debugging tool called...
New
sona11
What is the difference between tuple relational calculus (TRC) and domain relational calculus (DRC)? What distinguishes them from relatio...
New
harwind
I’m currently working on a front-end development project and I’m facing an issue with aligning items using CSS Flexbox. I want to horizon...
New
pjamesrud
I am creating an app that allows user to enter values and depending on the value a number of textviews are changed programmatically from ...
New
Clintonsuck
Hello, I am new and trying to build my first app. So far, everything was going okay, but now I’m stuck and don’t know how to proceed. May...
New
hosseinkhosromanesh
hello , i should code a cluster like image bellow we have no challenge in coding backend but in front need some clue to do this its a dy...
/js
New

Other popular topics Top

DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
New
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New