Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Pass an array starting at an offset to a function

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably Jeff , Mathew & Myself were posting simultaneously.
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 69738
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic