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?

Popular Frontend topics Top

PragmaticBookshelf
Database-driven sites bring complexity you might not need, but building a site by hand is too much work. Hugo is a static site generator ...
New
PragmaticBookshelf
JavaScript is no longer to be feared or loathed—the world’s most popular and ubiquitous language has evolved into a respectable language....
New
PragmaticStudio
Let’s get real. As in really knowing—clearly and practically—what’s up with Phoenix LiveView. What is it? How does it work? What can I ...
New
bot
Vue.js v2.6.11 has been released. Link: Release v2.6.11 · vuejs/vue · GitHub
New
AstonJ
DHH might like you to believe they are a thing of the past: https://twitter.com/dhh/status/1275902566719610880 I actually went down thi...
New
CommunityNews
Tailwind CSS Tools For Developers. A collection of tools designed for developers, to save you and your team time when building websites w...
New
mafinar
I am looking forward to the talks and the new release, I’ve had Alpine on my radar for awhile now, I will probably slowly introduce it in...
New
New
First poster: bot
Yew yew-v0.19.3, yew-router-v0.16.0 and yew-agent-v0.1.0 has been released. Link: Release yew-v0.19.3 · yewstack/yew · GitHub Link: R...
New
Fl4m3Ph03n1x
Background I have a button that may be disabled or not, depending on a set of conditions. I want to disable/enable the button without hav...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1017 16953 374
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
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
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New