aspose file tools
The moose likes Beginning Java and the fly likes Java array realization question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Java array realization question" Watch "Java array realization question" New topic
Author

Java array realization question

angel one
Greenhorn

Joined: Nov 16, 2009
Posts: 4
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

Joined: Mar 24, 2008
Posts: 3670

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):


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Welcome to the Ranch

What do you think?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
I think what the question means, is "how are arrays accessed by the computer's addressing?"
angel one
Greenhorn

Joined: Nov 16, 2009
Posts: 4
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
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
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

Joined: Mar 24, 2008
Posts: 3670

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

Joined: Nov 16, 2009
Posts: 4
Thank you guys

But i lost the bet
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
So what was your answer?
angel one
Greenhorn

Joined: Nov 16, 2009
Posts: 4
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
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Java array realization question
 
Similar Threads
Bubble sort
How to sort objects in a Collection based on method's name
test for valid characters
Doubts with Iterator
ArrayList vs. LinkedList