This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Pass an array starting at an offset to a function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pass an array starting at an offset to a function" Watch "Pass an array starting at an offset to a function" New topic
Author

Pass an array starting at an offset to a function

Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
If I have an array like so:



and I have a function:

and I want to pass this to a function from index position 2 (pseudocode), ie



How would I achieve such a thing?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4370
    
    8

I'd suggest just passing the offset to the function as a separate argument. You can't do pointer arithmetic on a Java array, so you can't change it's size or starting point without copying to a completely new array.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If I understand correctly, you want to tell the method where to start and how many to process, yes? So you need to add a parameter to the method to indicate the start position, and then you pass the array, the start parameter, and the end parameter. Note that you cannot pass part of the array. You're actually passing the array, just a reference to it. Objects are never passed to methods in Java.

Then inside the method, you'll need to modify your for loop to start at the desired position instead of at 0.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I am not sure if I understood your question correctly. Am answering assuming, you want to retreive starting from second element.

Probably you could have a third parameterin the function and pass the element from which you want to iterate. Modifying your code



And, you call the function as below




Ashwin Sridhar
SCJP | SCWCD | OCA
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Probably Jeff , Mathew & Myself were posting simultaneously.
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Matthew Brown wrote:I'd suggest just passing the offset to the function as a separate argument. You can't do pointer arithmetic on a Java array, so you can't change it's size or starting point without copying to a completely new array.


I guessed that you would need to add an offset parameter. I just wondered if there was any way to specify an offset of an array like arr+2 or arr[2].

I suppose as the array object contains a length, then passing part of the array would invalidate the length so wouldn't make sense.

Thanks for all your replies.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Matthew Brown wrote:I just wondered if there was any way to specify an offset of an array like arr+2 or arr[2].


2

I suppose as the array object contains a length, then passing part of the array would invalidate the length so wouldn't make sense.


Again, remember, we're not passing the array, we're passing a reference to the array. You can't pass a reference to part of an array any more than you can pass a reference to part of any other object.
Angus Comber
Ranch Hand

Joined: Jul 16, 2011
Posts: 90
Jeff Verdegan wrote:Again, remember, we're not passing the array, we're passing a reference to the array. You can't pass a reference to part of an array any more than you can pass a reference to part of any other object.


The fact that it is a reference I don't think is the issue. What is a reference? Just an alias for the actual object. In some languages you can specify an offset to an array.

Eg array[offset] - will reference start of array then increment no. elements by offset specified.

But the Java 'array' is not really a section of memory like say an array in C, it is a class (I suppose). So the start of the Java array is the start memory location of the object, not the direct location in memory of the first element.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Angus Comber wrote:
Jeff Verdegan wrote:Again, remember, we're not passing the array, we're passing a reference to the array. You can't pass a reference to part of an array any more than you can pass a reference to part of any other object.


The fact that it is a reference I don't think is the issue. What is a reference? Just an alias for the actual object.


No, it's not an alias, and the fact that it's a reference to an object is precisely the issue. The reference is a pointer to the object. If you're familiar with C++, a Java reference is much more like a C/C++ pointer than a C++ reference. A C++ reference is often referred to as an alias for a variable, but Java's references are nothing like that.

In some languages you can specify an offset to an array.


Not in Java. In java a reference to an array is just like a reference to any other object, and a reference to an array is not simply a pointer to its first element.

But the Java 'array' is not really a section of memory like say an array in C, it is a class (I suppose).


Correct. It's probably implemented that way in the JVM, but it's not specified as such in the JLS, so Java programmers have no visibility into that bit.

So the start of the Java array is the start memory location of the object, not the direct location in memory of the first element.


Not even that. There is no "start" of the array, just a reference to it, and I think you'll find that the JLS doesn't define references in terms of memory locations.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
An array is an object (not a class), and it does say so in the JLS. You can even find out the type or class name. Arrays do not necessarily (as in C) represent a contiguous block of code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass an array starting at an offset to a function