This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to divide an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to divide an array" Watch "How to divide an array" New topic
Author

How to divide an array

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
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

Joined: Aug 18, 2006
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++;
}


..............................<br />Exceptions are a part of possibility<br />Errors are a part of truth<br />................................
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Aug 18, 2006
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

Joined: Oct 30, 2001
Posts: 1970
Please read HowToAnswerQuestionsOnJavaRanch.

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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Nov 08, 2007
Posts: 457
would certainly be better if coded in Lists, instead of arrays


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
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

Joined: Oct 14, 2006
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.


Prateek<br />SCJP 1.5
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!


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


[ January 17, 2008: Message edited by: Darren Alexandria ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to divide an array