This week's book giveaway is in the Performance forum.We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!

ilteris kaplan
Ranch Hand
Posts: 38
So right now I have an array of 15 words. I am looking for a way to create a new array which consists of those 15 words in quantities of five.

myoldarray={word1,word2,word3...word15}

so my new array should be something like this:
mynewarray = {"word1,word2,word3,word4,word5","word6,word7,word8,word9,word10",
"word11,word12,word13,word14,word15"};

how is this possible?

best
ilteris

Dave Jones
Ranch Hand
Posts: 77
I don understand what is it exactly that you are asking ? can you give a small example ?

Do you want to create a two "dimmention" array ?
Or just group together every five "words" to an array of size three ?
[ April 16, 2006: Message edited by: Dave Jones ]

ilteris kaplan
Ranch Hand
Posts: 38
no it is nothing to do with 2d arrays actually. right now I have this long string and I want to divide into an array but not as one word each but like 4-5 groups of words long.

so if I do this:

String myArray[] = mystring.split(" ");

since it splits every gap, it gives me an array full of words, how can I make this split every 4 or 5 group of words..?

am I making sense?

thanks
[ April 16, 2006: Message edited by: ilteris kaplan ]

Dave Jones
Ranch Hand
Posts: 77
I don't know of a way to "size" the splitting, one can limit a splite but that won't help you here.
I think the easiest and best way will be to split it as you did, and then create the new array you wanted from that array (the 15 sized one) .
[ April 16, 2006: Message edited by: Dave Jones ]

ilteris kaplan
Ranch Hand
Posts: 38
the tricky part is I necessarily don't know how many elements I have in the oldarray, so I am looking for a generic way of cutting and pasting without any remainders.

maybe it would be better if I put the oldarray into arraylist and then remove elements as adding to new array.

what do you guys think?

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Think how you'd do this with pencil & paper ...

Break the sentence into words and count them.

Pick a chunk size, say 5.

Figure out how many chunks you're going to have. The last chunk might be short but you still have to count it.

Take the first n words and copy them to chunk 1.

Take the next n words and copy them to chunk 2.

repeat until done ...

With pencil & paper you don't remove words from the list, you just move your finger along the list to point at the next word to copy. An array index does the very same thing. You know you're done when your finger goes past the end of the list. How do you check for an array index going past the end?

With pencil & paper you probably don't care how many chunks you're going to make. You just start a new line for each one. Have you thought about where you're going to put your chunks? If you put them into an array (the two dimensional array idea from above) you'll have to know the count to make the array. If you put them into a List or Set you won't have to know ahead of time and you can skip that step.

BTW: To count the words in the list use split to get an array and see how long the array is.

Give it a shot in Java. Post what you come up with!
[ April 16, 2006: Message edited by: Stan James ]

ilteris kaplan
Ranch Hand
Posts: 38
ok this was a challenge, I did a very bad hack here as you'll see in the comments of the code. did I succeed? Nope, I am missing the last elements if my array is consisted of odd numbers, or numbers that is not dividable by the chunk iterator.

here we go:

what do you guys think?
thanks

ilteris kaplan
Ranch Hand
Posts: 38
if(k==8 || firstArrayList.size()==1) {

adding this fixed it. I am still looking for easier ways of doing this..

Henry Wong
author
Marshal
Posts: 21212
81
You do know that you can iterate backwards, right???

You can do this...

Henry

Henry Wong
author
Marshal
Posts: 21212
81
Now I am a bit confused... You iterate to insert the array into the firstArrayList backwards, so that you can later iterate backwards on the firstArrayList to extract out the original elements?

Wouldn't it be better to just work forwards? ... Or better yet, wouldn't it be better to just use the myarray directly? Skipping the need for the firstArrayList entirely?

Just wondering...
Henry

Justin Fox
Ranch Hand
Posts: 802
all you would have to do is take the array, have a method with a variable that increments as you count up to while(count <= 3), because you could have 20 , 6 or 8 words....

then just take

int index = 0;
int count = 0;
for(int i = index;i<newarray.length;i++)
{
while(count <= 3)
{
String word += "array[i]";
count ++;
}
newarray[i] = word;
}

i believe that would group em together....

post if im wrong

Monk...

ilteris kaplan
Ranch Hand
Posts: 38
it makes totally sense. my only question would be how do you define the length of the newarray?

Justin Fox
Ranch Hand
Posts: 802
well my friend....if you have an array say with length 13

so the first time around

you'll have 3 strings concatinated into one at newArray[i].

so when your done you'll have

newArray [] = {3 words,3 words,3 words,3 words,1 word(s)};

so no matter what, the length of the newArray will always be:

int [] newArray = new int [((oldArray.length)/3)+1];

hope this helps,

Fox...

ilteris kaplan
Ranch Hand
Posts: 38
thanks for the patience

It is total clear for me now. thank you!

update: well on a second look when I try to test the code below it gives me 6 elements 3 times.
word1 word2 word3 word4 word5 word6
word1 word2 word3 word4 word5 word6
word1 word2 word3 word4 word5 word6

[ April 17, 2006: Message edited by: ilteris kaplan ]

ilteris kaplan
Ranch Hand
Posts: 38
and this works at lasst

Fox Trot
Greenhorn
Posts: 5
this is Monk, glad you figured it out man, good for you...

Monk...

-I've lost Myself-