简体   繁体   English

如何使用Java实现类似数组的数据结构?

[英]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. 我试图找出是否可以尝试使用Java实现类似数组的数据结构,但我做不到。

I've searched online but didn't find anything useful. 我已经在网上搜索过,但没有发现任何有用的信息。

Is it even possible to implement an array-like data structure in Java? 甚至有可能在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. 数组是内存中的连续部分,因此要创建数组,您需要保留一块大小为n * sizeof(type)的内存,其中n是您要存储的项目数量以及sizeof(type)将返回大小,以字节为单位,JVM需要用字节来表示该给定类型。

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)) . 然后,您将对内存段的第一个位置(例如0x00 )存储一个引用(指针),然后将其用作基础来了解访问元素所需的移动量,因此a[n]等于做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. 尝试在Java中实现此问题的原因是Java不允许指针操作,因此我认为无法构建自己的数组类型,因为您无法降低到该级别。

That being said, you should be able to create a linked data structure, where the nth element points to the (n + 1)th element. 就是说,您应该能够创建一个链接数据结构,其中第nth元素指向第(n + 1)th元素。

Other problems why you should try other languages, such as C# (check unsafe operations), C++ or C : 为什么您应该尝试其他语言的其他问题,例如C# (检查unsafe操作), C++C

  1. To my knowledge, Java does not have a sizeof function (see this ). 据我所知,Java不具有sizeof函数(见 )。
  2. Java does not allow operator overloading . Java不允许operator overloading So you cannot define your own indexing operators such as [index] . 因此,您无法定义自己的索引运算符,例如[index] You would probably need to do something like array.getElementAt(0) to get the first element. 您可能需要执行诸如array.getElementAt(0)才能获取第一个元素。

As @ug_ recommended, you could take a look at the Unsafe class. 按照@ug_的建议,您可以看一下Unsafe类。 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. 然后,您将无法使用Java执行此操作,但是可以使用其他语言。 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. 在Java中并不是真正的结构,但是您可以实现它们。

Read up on Structs here: www.cplusplus.com/doc/tutorial/structures/ 在此处阅读有关Structs的内容:www.cplusplus.com/doc/tutorial/structures/

An example Structs used in java: Creating struct like data structure in Java Java中使用的示例结构:在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/ 载体信息: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 数组与向量的比较: 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 链接列表: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. 有点开放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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