Re: Q about java arrays
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/30/03
- Previous message: Michael G: "Re: AWTEvents errors and deprecated methods"
- In reply to: johnny: "Q about java arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 30 Nov 2003 05:56:50 GMT
"johnny" <squidish@hotmail.com> wrote in message
news:oYdyb.73078$Vs1.54284@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
- Previous message: Michael G: "Re: AWTEvents errors and deprecated methods"
- In reply to: johnny: "Q about java arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|