By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Q about java arrays

P: n/a
hello.
in C++, you can fake a sub-array by passing something
like (array + int). If this was passed to something that
takes an array as a parameter, the function will deal
with the array using the int as the starting value.
for example....

if we have some function with the declaration
void someFunctionOnSomeArray(int[] a)

and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

and then we call it with
someFunctionOnSomeArray( data + 5 );

the function will have as the array "a" the values
{ 6,7,8,9,0 }

My Question:
Is there a way to do the same thing in java?

thanks
~johnny

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"johnny" <sq******@hotmail.com> wrote in message
news:oY*******************@twister.austin.rr.com.. .
hello.
in C++, you can fake a sub-array by passing something
like (array + int). If this was passed to something that
takes an array as a parameter, the function will deal
with the array using the int as the starting value.
for example....

if we have some function with the declaration
void someFunctionOnSomeArray(int[] a)

and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

and then we call it with
someFunctionOnSomeArray( data + 5 );

the function will have as the array "a" the values
{ 6,7,8,9,0 }

My Question:
Is there a way to do the same thing in java?


In C++ an array [single dimension arrays - what may appear to be
multi-dimensional arrays are often fudged using pointers], whether stack or
heap based, is merely a contiguous chunk of memory [even when each element
is, itself, a fully-fledged object]. This, together with the ability to
obtain, and use, the address of arbitrary memory locations, allows the
practice you mention.

In Java an array is an object, and each element of the array is, itself, a
separate entity. Even if each element were contiguously located in memory
[something potentially only useful for primitive-type arrays anyway] there
is no way to obtain the address of arbitrary memory locations. So the answer
is: no.

You would have to rewrite your method [C++ has member data and member
functions, Java has fields and methods] to include a processing start
location:

void someFunctionOnSomeArray(int [] data, int start);

or perhaps specify a range:

void someFunctionOnSomeArray(int [] data, int start, int end);

For better or worse, many of the tricks and shortcuts common in C and C++
code must be abandoned, and a more Java-oriented mindset adopted. This takes
time, and determination.

I hope this helps.

Anthony Borla
Jul 17 '05 #2

P: n/a
Dans l'article <oY*******************@twister.austin.rr.com>, "johnny"
<sq******@hotmail.com> a tapoté :
in C++, you can fake a sub-array by passing something like (array +
int) .... and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 }; .... the function will have as the array "a" the values { 6,7,8,9,0 } .... My Question:
Is there a way to do the same thing in java?


AFAIK, there's no way to pick a slide from an array, for instance like
that: int[] data = {1,2,3,4,5,6,7} ; getting data[5..data.length-1] is
impossible.

Maybe someone will explain this java drawback.

Anyway, I think it's never a good idea to use primitive types when using
Objects is possible. I've never been confortable about this primitive-object
mix of java. When you read the api, it sometimes look like Sun guys think
everybody have an infinite amount of RAM, but hey, they still use 'int'
as constants instead of static instances. Why ? Saving a few kBytes ?
--
--
A day without sunshine is like a day without orange juice.
----------------------------
jerome.eteve_at_it-omics.com
Jul 17 '05 #3

P: n/a
SMC
On Tue, 02 Dec 2003 20:10:27 +1100, Jéjé wrote:
Dans l'article <oY*******************@twister.austin.rr.com>, "johnny"
<sq******@hotmail.com> a tapoté :
in C++, you can fake a sub-array by passing something like (array +
int)

...
and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

...
the function will have as the array "a" the values { 6,7,8,9,0 }

...
My Question:
Is there a way to do the same thing in java?


AFAIK, there's no way to pick a slide from an array, for instance like
that: int[] data = {1,2,3,4,5,6,7} ; getting data[5..data.length-1] is
impossible.


An array splice (of sorts):

int[] original = { 1, 2, 3, 4, 5 }
int[] splice = new int[3];

System.arraycopy(original, 0, splice, 0, 3);

Would give splice = { 1, 2, 3 }

--
Sean

"There are 10 types of people in this world, those who can count in binary,
and those who can't."
Jul 17 '05 #4

P: n/a
johnny wrote:
hello.
in C++, you can fake a sub-array by passing something like (array +
int). If this was passed to something that takes an array as a
parameter, the function will deal with the array using the int as the
starting value.
for example....

if we have some function with the declaration
void someFunctionOnSomeArray(int[] a)

and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

and then we call it with
someFunctionOnSomeArray( data + 5 );

the function will have as the array "a" the values
{ 6,7,8,9,0 }

My Question:
Is there a way to do the same thing in java?


As many have already indicated, you can't do this with arrays. However,
the java.util.List.subList() method provides a similar functionality.
So, if you are willing to work with Lists, you can use this methodology.
The drawback is that you cannot put primitives in Lists.

Ray

Jul 17 '05 #5

P: n/a
> int[] original = { 1, 2, 3, 4, 5 }
int[] splice = new int[3];

System.arraycopy(original, 0, splice, 0, 3);

Would give splice = { 1, 2, 3 }


Good old C style :)

--
--
Agree with them now, it will save so much time.
----------------------------
jerome.eteve_at_it-omics.com
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.