简体   繁体   中英

How to implement an array-like data structure using Java?

I know what arrays are and how to use them. However, I don't know how they are implemented. I was trying to figure out if I can try to implement an array-like data structure using Java but I couldn't.

I've searched online but didn't find anything useful.

Is it even possible to implement an array-like data structure in Java? Is it possible in other languages? if so how (without using arrays of course)?

EDIT: what I want to know is how to implement an array data structure without using arrays?

Arrays are contiguous sections within memory, so to create an array you would need to reserve a chunk of memory which is of size n * sizeof(type) , where n is the amount of items you would like to store and the sizeof(type) would return the size, in bytes which the JVM would need to represent that given type.

You would then store a reference (pointer) to the first location of your memory segment, say 0x00 , and then you use that as a base to know how much you need to move to access the elements, so a[n] would be equal to doing 0x00 + (n * sizeof(type)) .

The problem with trying to implement this in Java is that Java does not allow pointer manipulation, so I do not think that building your own array type would be possible since you cannot go down to that level.

That being said, you should be able to create a linked data structure, where the nth element points to the (n + 1)th element.

Other problems why you should try other languages, such as C# (check unsafe operations), C++ or C :

  1. To my knowledge, Java does not have a sizeof function (see this ).
  2. Java does not allow operator overloading . So you cannot define your own indexing operators such as [index] . You would probably need to do something like array.getElementAt(0) to get the first element.

As @ug_ recommended, you could take a look at the Unsafe class. But also as he recommended, I do not think that you should do pointer arithmetic with a language which has pointer abstraction as one of its core ideas.

If what you want is something like this:

MyArray ma = new MyArray(length);
ma[0] = value;

Then you can't do this in Java but you can in other languages. Look for "operator overloading".

I'm wondering if your thinking of a structs, vectors or link lists. These are all similar to arrays but are different.

Structs are not really in java, but you can implement them.

Read up on Structs here: www.cplusplus.com/doc/tutorial/structures/

An example Structs used in java: Creating struct like data structure in Java

I think what you are really looking for though are vectors. They are very similar to an array, but their not one.

Vectors info: www.cplusplus.com/reference/vector/vector/

Array compared to vector: https://softwareengineering.stackexchange.com/questions/207308/java-why-do-we-call-an-array-a-vector

I recommend a link list. Its kinda the same idea of an array, but without knowing your exact size. It is easier to implement.

Link lists: en.wikipedia.org/wiki/Linked_list

All these come down to the situation on what need them for. Saying , "what I want to know is how to implement an array data structure without using arrays?" is kinda open ended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM