• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java array realization question  RSS feed

 
angel one
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.
I have a dispute with my friend about a random element access in array.

We have to variants:
1. We know address in memory of array start and offset of needed element, make a sum of this and you get an offset of element.
2. By subsequent access to all elements. For example we need a 5th element, we subsequently need to get a 1st, 2nd, 3rd and 4th elements.

Thank you.
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I'm not sure what you are looking for, In Java you can access an array by using the reference to that array like this (assume you have a array of type int):
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What do you think?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what the question means, is "how are arrays accessed by the computer's addressing?"
 
angel one
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for greeting, but i just lost my old account and this is why i made new.

The question was about realization of arrays in java.
As far as know in C arrays are addressed by start offset and number of element multiplied by size of datatype.
How this works in java?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you plan to find out? the Java Language Specification didn't say much when I looked. What about the Java™ Tutorials? If that doesn't help, can you think of a way to try it out?
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, JLS leaves that up to the system implementaters, however typically elements of array occupy consecutive memory locations, and an array object contains additional length field.
 
angel one
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys

But i lost the bet
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what was your answer?
 
angel one
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:Yes, JLS leaves that up to the system implementaters, however typically elements of array occupy consecutive memory locations, and an array object contains additional length field.


Me and my friend, we both think that would be not efficient to use other implementation than specified by Vijitha.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote: . . . an array object contains additional length field.
It does in Java (and I think also C#) but not in C, where arrays are not actually objects.

Most arrays are indexed by relative indexing, which can be implemented in the CPU directly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!