473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I have two arrays.

27 New Member
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?
Jul 31 '08 #1
14 1497
MrHuggykins
27 New Member
Bump
Up
My
Post?
Jul 31 '08 #2
blazedaces
284 Contributor
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?

-blazed
Jul 31 '08 #3
MrHuggykins
27 New Member
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.
Aug 1 '08 #4
r035198x
13,262 MVP
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.
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.
By taking values out of an array do you mean resetting the array values to default values?
Aug 1 '08 #5
JosAH
11,448 Recognized Expert MVP
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.
Yoohoo! Don't forget to take your coffee! ;-)

kind regards,

Jos
Aug 1 '08 #6
r035198x
13,262 MVP
Yoohoo! Don't forget to take your coffee! ;-)

kind regards,

Jos
And 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.
Aug 1 '08 #7
MrHuggykins
27 New Member
And 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.
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?

Edit: Remember I need to write a test class, so this has to be in method-form.
Aug 1 '08 #8
blazedaces
284 Contributor
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?

Edit: Remember I need to write a test class, so this has to be in method-form.
Why don't you just fill the array with a value, like make all the numbers in an int array = 0 again?

-blazed
Aug 1 '08 #9
MrHuggykins
27 New Member
Look at the title in under my name.
Aug 1 '08 #10
JosAH
11,448 Recognized Expert MVP
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 think that you don't really understand what an array is; if I do this:

Expand|Select|Wrap|Line Numbers
  1. T[] array= new T[42];
  2.  
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,

Jos
Aug 2 '08 #11
MrHuggykins
27 New Member
I think that you don't really understand what an array is; if I do this:

Expand|Select|Wrap|Line Numbers
  1. T[] array= new T[42];
  2.  
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,

Jos
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 =(
Aug 3 '08 #12
Laharl
849 Recognized Expert Contributor
No, you can't directly resize an array. If you want to do that, use an ArrayList or Vector (Google/the API is your friend).
Aug 3 '08 #13
blazedaces
284 Contributor
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 =(
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...

I'll check periodically...

-blazed
Aug 3 '08 #14
JosAH
11,448 Recognized Expert MVP
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 =(
Yep, arrays are kindof stupid; if you want an array of another size you have to
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,

Jos
Aug 3 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

19
2816
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
3891
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
29227
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
2816
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
8684
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
4873
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
13129
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
2431
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
2503
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
35375
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.