two Object[] into one

R

RVic

If I have Object array1[] and Object array2[] , how can I make Object
array3[] without copying over each element from each array into
array3 ? Thanks.
 
S

Simon

If I have Object array1[] and Object array2[] , how can I make Object
array3[] without copying over each element from each array into
array3 ? Thanks.

With arrays, this is impossible. Note, however, that in Java, you are
only copying references, which is relatively cheap. System.arraycopy()
does this for you.

If that does not suit your needs, think about implementing something
like a Union data structure inheriting from Collection or List which is
a view of two underlying lists, Collections, or arrays.

Cheers,
Simon
 
A

Arne Vajhøj

RVic said:
If I have Object array1[] and Object array2[] , how can I make Object
array3[] without copying over each element from each array into
array3 ? Thanks.

If you want array3 to contains all elements from both array1 and
array2, then that requires moving data, because arrays are
stored sequentially in Java.

You can easily make array3 an array that has two elements
array1 and array2.

Arne
 
R

Roedy Green

If I have Object array1[] and Object array2[] , how can I make Object
array3[] without copying over each element from each array into
array3 ? Thanks.

The closest thing you could do is this:

array[][] array3 = new Object[2];
array3[0] = array1;
arry3[1] = array2;

But if you modify array3, you will also be modifying array1 or array2
since there is only one copy of the array1/array2 references.

Single dimensioned arrays are always contiguous in ram, so to get an
array3[] you are going to need to copy elements.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Let us pray it is not so, or if it is, that it will not become widely known."
~ Wife of the Bishop of Exeter on hearing of Darwin's theory of the common descent of humans and apes.
 
D

Daniel Pitts

RVic said:
If I have Object array1[] and Object array2[] , how can I make Object
array3[] without copying over each element from each array into
array3 ? Thanks.
Magic or luck, but not any other way.
 
A

Arne Vajhøj

Christian said:
RVic said:
If I have Object array1[] and Object array2[] , how can I make Object
array3[] without copying over each element from each array into
array3 ? Thanks.

simple task

Object[] array3 = new Object[0];

no need to copy over anything..

Make a guess about what the original poster wanted.

Arne
 
L

Lew


Man, am I ever glad for that link. Christian completely lost me with
that obscure abbreviation. I thought it was a signature, like his
initials, or something. Thanks, Roedy.

If I had understood "leetsprech" or whatever execration "scnr"
represents, I would have been LMFAOOL, no doubt.

BTW, even without the explicit hint that his comment was intended for
humor, Christian's suggestion communicated dry wit. The content
provided the context.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top