And I need to take the elements from array1 and put them at the end of array2. array1 will need to be empty afterwards, so no copying. Ideas? Or is there somewhere I can go to read about it?
I have two arrays.
Collapse
X
-
Tags: None
-
-
I don't understand what's the problem? You know how to put a value in an array right? Why don't you just put the values in array1 ... in the values of array2 (at the end or wherever you mean) using a loop of some kind?
-blazedComment
-
The problem is I'm writing a method and the test program has to be able to take the elements OUT of the first array and put them into another.Comment
-
If you want to pass an array to a method and expect that that array be changed after the method call then you can't do that. Java passes by value so a copy of the array will be created when you call the method and changes made in the method are done to that copy not to the original array.Originally posted by MrHuggykinsThe problem is I'm writing a method and the test program has to be able to take the elements OUT of the first array and put them into another.
By taking values out of an array do you mean resetting the array values to default values?Comment
-
Yoohoo! Don't forget to take your coffee! ;-)Originally posted by r035198xIf you want to pass an array to a method and expect that that array be changed after the method call then you can't do that. Java passes by value so a copy of the array will be created when you call the method and changes made in the method are done to that copy not to the original array.
kind regards,
JosComment
-
And right on cue the coffee comes.Originally posted by JosAHYoohoo! Don't forget to take your coffee! ;-)
kind regards,
Jos
@OP You can forget the crap I wrote about not being able to change the array's values. Arrays are pointers and passing a pointer to an array to a method allows you to modify that array (through the pointer).
I think I'll right an article about this so my fingers don't keep forgetting it.
Thanks Jos.Comment
-
Well see.. I guess you'd copy them to the new array and then remove them from the first array. How would you do that?Originally posted by r035198xAnd right on cue the coffee comes.
@OP You can forget the crap I wrote about not being able to change the array's values. Arrays are pointers and passing a pointer to an array to a method allows you to modify that array (through the pointer).
I think I'll right an article about this so my fingers don't keep forgetting it.
Thanks Jos.
Edit: Remember I need to write a test class, so this has to be in method-form.Comment
-
Why don't you just fill the array with a value, like make all the numbers in an int array = 0 again?Originally posted by MrHuggykinsWell see.. I guess you'd copy them to the new array and then remove them from the first array. How would you do that?
Edit: Remember I need to write a test class, so this has to be in method-form.
-blazedComment
-
-
I think that you don't really understand what an array is; if I do this:Originally posted by MrHuggykinsAnd I need to take the elements from array1 and put them at the end of array2. array1 will need to be empty afterwards, so no copying. Ideas? Or is there somewhere I can go to read about it?
[code=java]
T[] array= new T[42];
[/code]
I end up with 42 adjacent slots and each slot can contain a T, whatever that T is;
if T is a primitive (int, double etc). the slots of the new array have all their bits set
to zero which effectively results in values 0 or 0.0 or 0L for the primitive type T.
If T is some type of object (a class or interface or enum) all the bits of all the slots
also have their bits set to zero which effectively results in the value 'null' which
means that none of the slots point to a type T, whatever T happens to be.
But whatever you do you always have those 42 slots until you forget about the
entire array altogether, i.e. you can't add slots nor can you remove slots from the
array. It is one chunk of 42 adjacent slots with index values 0, 1, 2 ... 41
Your remark about 'empty arrays' and 'no copying' is confusing at best and doesn't
make any sense at worst. The only thing you can do with those slots in an array
is change their value or copy their value to somewhere else.
kind regards,
JosComment
-
So let me get this straight.. if you make that T[42] you're saying there's NO WAY to resize an array? Also, can anyone maybe give me some live help? I really need to get this done =(Originally posted by JosAHI think that you don't really understand what an array is; if I do this:
[code=java]
T[] array= new T[42];
[/code]
I end up with 42 adjacent slots and each slot can contain a T, whatever that T is;
if T is a primitive (int, double etc). the slots of the new array have all their bits set
to zero which effectively results in values 0 or 0.0 or 0L for the primitive type T.
If T is some type of object (a class or interface or enum) all the bits of all the slots
also have their bits set to zero which effectively results in the value 'null' which
means that none of the slots point to a type T, whatever T happens to be.
But whatever you do you always have those 42 slots until you forget about the
entire array altogether, i.e. you can't add slots nor can you remove slots from the
array. It is one chunk of 42 adjacent slots with index values 0, 1, 2 ... 41
Your remark about 'empty arrays' and 'no copying' is confusing at best and doesn't
make any sense at worst. The only thing you can do with those slots in an array
is change their value or copy their value to somewhere else.
kind regards,
JosComment
-
Send me a PM and I'll try my best to help you out. I'll try my best to guide you through this if you would like...Originally posted by MrHuggykinsSo let me get this straight.. if you make that T[42] you're saying there's NO WAY to resize an array? Also, can anyone maybe give me some live help? I really need to get this done =(
I'll check periodically...
-blazedComment
-
Yep, arrays are kindof stupid; if you want an array of another size you have toOriginally posted by MrHuggykinsSo let me get this straight.. if you make that T[42] you're saying there's NO WAY to resize an array? Also, can anyone maybe give me some live help? I really need to get this done =(
create it, haul the elements over from the first array and forget the first array.
btw, that's exactly how the ArrayList does it.
kind regards,
JosComment
Comment