aspose file tools*
The moose likes Beginning Java and the fly likes array copy question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array copy question" Watch "array copy question" New topic
Author

array copy question

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
is this code:

the same as:

?
thanks in advance
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
No. For starters, the second code sample doesn't even compile, so that would be one significant difference. I think I see what it's supposed to do, but I'm always deeply suspicious of code that uses random indentation like that. Why is "myArray[index] = array;" indented in both code samples? Is it supposed to be looped over somehow?
Am I correct in guessing that you're trying to insert a single element into an array, shifting the subsequent elements one to the right? The first code should do that. The second sample will not. Aside from bad formatting and a missing end brace, there is a problem with the beginning and end of the last loop. What index should the loop really begin at? What's the first postion that needs to be shifted? And what index should it end at? Remember that in the first code sample with arrayCopy(), (myCt-index) represented the length of portion of the array that needed copying. It was the difference between the start and stop positions. (At least I assume it was, having never seen the definition of myCt or index.) But that's not what you need in the last loop with k.
Incidentally, I don't know what myCt is. I assume it's something like the length of the part of myArray that we care about. I hope it's not the lenght of the whole array though, because when you insert one new element into the array, there's no room at the end to shift the last element. Unless you want (or at least plan) to lost the last element, which is OK too as long as it's intentional. Otherwise I hope you're ensuring that (myCt + 1 < myArray.length), or you're going to have problems here. Even in the first code sample, which is otherwise OK.
Also, if you read the arraycopy() API you'll see it's designed to handle copying from an array to itself (at a different postition). So you can replace your code above with just two lines:

One other thing - is the variable "array" really a reference to an array? Or is it some other type of Object which is being inserted into an array? If it's the latter, then "array" is a needlessly confusing name for this variable.


"I'm not back." - Bill Harding, Twister
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hey Jim, thanks for the reply. Yes this is designed to insert an array (numlist) into another array: myArray at a specific index.
myCt is the number of elements in the array. As shown by the output below, cap is the output for the actual capacity(myArray.length). I am trying to use for loops instead of System.arraycopy, thus my original question. I know how to get the numlist array into myArray, but I am getting some pretty weird outputs.
after my addAll method, they are supposed to look like this:
v[2] = < 3, 2, 1, 1, 2, 3, 19, 0, 1, 2, 3 > (cap: 18)
v[4] = < 1, 2, 3, 19, 1, 2, 3, 19 > (cap: 8)
But they look like this:
v[2] = < 3, 2, 1, 1, 1, 2, 3, null, 2, 3, 19 > (cap: 18)
v[4] = < 1, 2, 3, 19, 1, 2, 3, 19 > (cap: 8)
basically I am to take the vector and insert 1,2,3,19 into the existing array and shift all of the elements to the right. I am not worried about V[4] because it is working. V[2] is giving me the problems. It is the one that I am supposed to shift the existing elements to the right.
Here is my code from main:


my v[2] allAll method is as follows:


My ensureCapacity method basically doubles the array size if required to add more elements, and it works fine.
I just cant seem to figure out a way to shift the elements over to the right when I insert the numlist at index.
Thanks for your info Jim.
Steve
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
This was supposed to be a question about the for loops.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
To shift elements in the array, try starting from the other end. That is, first move the last element in the array, then the next to last, etc. This way you should be able to free up space before you need to copy something else into the same space.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Ok, thanks for the reply Jim. I'll give it a try tomorrow and let you know if it worked.
Thanks again.
Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array copy question