java中两种不同类型的索引数组
我需要一个数组类型来存储对象。但我需要两种类型的访问属性,如下所示:
array[0] >>> object1
数组["a"] >>> object1
这意味着索引 0(整数)和索引 a(字符串)取消引用数组中的同一对象。为了存储对象,我认为我们需要集合,但是我如何访问上面提到的属性?
I need an array type for storing objects. But i need two types of access property like this:
array[0] >>> object1
array["a"] >>> object1
This means index 0 (an integer ) and index a (a string) dereferences same object in the array. For storing objects, i think we need collections but how can i do access property that i mentioned above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建从字符串键到数字键的映射:
然后创建对象列表,其中 MyObject 是值类型:
通过整数访问:
通过字符串访问
这需要维护您的键数据结构,但允许从以下位置访问您的值:一种数据结构。
如果您愿意,您可以将其封装在一个类中:
Create a map from the string keys to the numeric keys:
Then create a List of your objects, where MyObject is the value type:
Access by integer:
Access by String
This requires maintaining your key data structure, but allows for access of your values from one data structure.
You can encapsulte that is in a class, if you like:
数组仅支持不确定性。
看来您能够通过索引和键查找对象。在这种情况下,我建议您有两个集合。每种类型的查找都有一个。
Arrays only support indecies.
It appears you what to be able to look up an object by index and by key. In which case I suggest you have two collections. One for each type of lookup.