• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java array realization question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What do you think?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys

But i lost the bet
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what was your answer?
 
angel one
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic