aspose file tools*
The moose likes Beginning Java and the fly likes Searching, copying,  and concatenating arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching, copying,  and concatenating arrays" Watch "Searching, copying,  and concatenating arrays" New topic
Author

Searching, copying, and concatenating arrays

Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
I have an array of exactly 50 integers. I need to search through the array until I find a value of either 49 or 50. As soon as one of these values is found, I want to export all of the values that came before to their own array.

Next, I want to go through the original array until the other value is found (i.e. if 50 was found before, go through array until 49 is found). I then want to export all of these values (starting with either 49 or 50 and ending with the other) to their own array.

Finally, I want to export the remaining values in the original array to their own array. Then, I want to concatenate all of these arrays. The last array will come first, the second will stay in the middle position, and the first one will come last.

Now, I have written a lot of code to do this, but I have ran into a lot of problems. First, I don't know how large to make the arrays. I think I can solve this by using an ArrayList, which I have used once before. But my main problem is being able to search through the array at the correct location. As I said before, I want the first exported array to stop as soon as either 49 or 50 is found. But I'm having trouble getting the next search to begin where the last one left off. Should I use the indexOf method to save where the value was found and then begin searching there?

I've done some research to figure out the best way to add these three arrays together, but I can't find any consistency. Should I use the arraycopy method or is there an easier way to do this?

Thanks a million. Any help is appreciated.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
An array doesn't have methods like indexOf associated with them, but you shouldn't have any trouble using an int to access elements in the array.

The arraycopy method is very useful for copying arrays.

You should be able to decide the length of the new arrays based on the positions of the values 49 and 50.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Do you need the intermediate arrays for anything other than temporary storage ?
If not, then I'd just create the final array, the same size as the original array and then copy the elements directly into it.
For example, for the elements up to the first instance of 49 or 50 : find the location of the first 49 or 50 value. Lets say this location is element 10. Use ArrayCopy to copy the first 10 elements of the original array into the last 10 elements of the new array.
Repeat for the other blocks.


Joanne
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
Joanne,

Thanks for the tip. That makes a lot more sense than what I was going to try. I tried doing that only for the first part, which is when I need to put all of the cards before 49 or 50 at the end of the array. What I have below only does that and does not copy it back to the original array yet.

For what I am trying to do, does that look right? For some reason, it's giving me ArrayIndexOutOfBoundsException.



Like I said, all the above function has to do at this point is find either 49 or 50 and move all cards before it (but not including it) to the end of the temporary array.

I feel like there's probably a more elegant way of finding where the first instance of either 49 or 50 is.

Any suggestions would be great. Thanks.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
1. You might want to search through the cards array 0 to < cards.length and first identify exactly where 49 and 50 are located and pop these into temp int variables. Then go back and use these variables to find the bounds to do your arraycopy (and you'll do more than one array copy).
2. Your while loop will always fail and keep going out of bounds since cards[i] != 49 || cards[i] != 50 will always be true.
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
Pete,

That's a good idea. Also, I see what you mean about my loop. I'm not sure why I even thought that would work.

As far as saving the locations of the values into temp variables, how do I go about searching the array? Do I simply loop through the array and create an if statement to find the variables? I thought about using binarySearch, but I read that it must be sorted beforehand, which isn't possible for what I'm doing.

Is there an easier way to do this? I'm thinking that there must be.

Thanks.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Philip Jenson:
As far as saving the locations of the values into temp variables, how do I go about searching the array? Do I simply loop through the array and create an if statement to find the variables? I thought about using binarySearch, but I read that it must be sorted beforehand, which isn't possible for what I'm doing.

Is there an easier way to do this? I'm thinking that there must be.

Thanks.


I don't think that you can binary search a non-sorted list or array. Besides sorting the array would defeat the purpose.
Use the KISS rule: You only have 50 items to go through so might as well step through them all one at a time and check each item just as you have suggested above. You're on the right track.

Let's see your code when your done,... and good luck.
Philip Jenson
Greenhorn

Joined: Jan 24, 2007
Posts: 21
This is my code so far, though I'm still working on it. I think I have the looping correct, but I'm having an awful time figuring out the destination position and number of elements for the arraycopy. I think I might have got the first one right, but I'm confused with the other two. I made the two lines giving me trouble bold and put question marks in the parameters that are giving me the most trouble.

I'm going to keep at it, but I'll check back to see if anyone understands what I'm having trouble with.


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Coming along there.
Couple of suggestions:
1) right now as written, first and second will both be set to the same number (the higher index location of 49 or 50). This needs to be changed.
2) System.arraycopy(cards, 0, tempArray, 50-first, first); looks good. To figure out where the other array copies go, draw out an abbreviated version of your arrays on paper and play with the numbers. It'll come to you.
3) Get rid of the "magic number" 50. Instead of your for-loop going until i < 50, have it loop until i < cards.length; This is just a good habit to get into. Same for your arraycopy calls -- where you have 50, use cards.length instead. If later on someone changes the array length to 100, your for loop will now automatically change as well. This will make your code much less prone to bugs.
[ March 22, 2007: Message edited by: pete stein ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching, copying, and concatenating arrays
 
Similar Threads
Fast text searching
Searching a 2d array
Sun Cirtification
Soo close Part II
Finding and changing array elements