Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# How to divide an array

Darren Alexandria
Ranch Hand
Posts: 185
Good day!

As of the moment, I have an array
like this:

int motherarray1[30] = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, are, s, t, you, v, w, x, y, z};

My task is to split/divide them into different arrays depending on the
value given by the user.

For example, the given interval for the array contents is 5, then from the mother array it should look like this:

int babyarray1[10] = {a, b, c, d, e, p, q, are, s, t}

int babyarray2[10] = {f, g, h, i, j, you, v, w, x, y}

int babyarray3[10] = {k, l, m, n, o, z}

Thanks!

Rohan Kalbhor
Ranch Hand
Posts: 78
int babyarray1[10] = {a, b, c, d, e, p, q, are, s, t}

int babyarray2[10] = {f, g, h, i, j, you, v, w, x, y}

int babyarray3[10] = {k, l, m, n, o, z}

int j=0;
for(int i=0;i<30
{
if(i<10)
{
babyarray1[i]=motherarray[j];
}
if(i>10&&i<20)
{
babyarray2[i]=motherarray[j];
}
if(i>20&&i<30)
{
babyarray3[i]=motherarray[j];
}
j++;
}

Campbell Ritchie
Sheriff
Posts: 48961
60
You would have to set up a new array as a recipient, with the requisite size, and use a for loop to iterate the arrays and copy the values from source array to target array. At least that's how you would do it for a class exercise.

In real life you would use the System.arraycopy() method.

BTW: Does your declaration of a 30-member array actually compile? You are declaring it with 30 members and initialising it with 26.

Campbell Ritchie
Sheriff
Posts: 48961
60
Thank you for replying, Rohan Kolbhar, but it is not the policy of the Ranch to give out simple answers like that. And what you posted doesn't actually achieve what is required. Suggest you edit your post and remove the answer.

I would do it with a int[][] arrays, using the / and % operators, but a lot of beginners would see that as very complicated programming.

Rohan Kalbhor
Ranch Hand
Posts: 78
Hi Campbell,

thanks for acquainting me with this policy.
I have just given an overall idea of the logic with code.I feel that this forum is for beginners.So most of the programmers have good logic but problems arise during coding.

I will avoid it next time.

Regards
Rohan

Peter Chase
Ranch Hand
Posts: 1970

Unfortunately, while some people asking for solutions do just need a bit of help with a solution they're working on, there are far too many people who just want a ready-made answer for their homework or job. We don't want to give them those ready-made answers, because they will progress in their courses or jobs without really gaining the necessary skills. And, one day, you or I might have to work with them...

So, by all means, help out, but do so in such a way that the person you're helping learns from the experience.

Campbell Ritchie
Sheriff
Posts: 48961
60
I first came across this problem this morning and it is now nearly 3pm. The more I think about it, the more difficult it appears to be to get an array into the format Darren appears to require. I can see how it can be done, but it by no means an easy problem to give beginners.

Bill Shirley
Ranch Hand
Posts: 457
would certainly be better if coded in Lists, instead of arrays

Darren Alexandria
Ranch Hand
Posts: 185
Thanks to everyone that replied and shared their ideas.
I am now going to implement this using List.

Thanks again!
God bless you all.

prateek chaudhari
Greenhorn
Posts: 11
I think there is no need of lists if the size of the array is limited to mazimum 10 elements, as shown in the question.

Its is very easy problem, the only thing it may require you to have a simple knowledge of "Ragged Arrays" which will help you.

Darren Alexandria
Ranch Hand
Posts: 185
Good day!

The maximum possible size that my task requires
is more than 4000.

[ January 17, 2008: Message edited by: Darren Alexandria ]