在Java中向类添加自定义数组下标访问器?

在Java中向类添加自定义数组下标访问器?,java,syntax,operator-overloading,Java,Syntax,Operator Overloading,在Python中,可以添加列表下标作为自定义类的数据结构的访问器: class customFile: # other methods ... def __getitem__(self, x): return self.list[x] 要获取的行为,请执行以下操作: newFile = customFile() newFile.list[1] = 4 newFile.list[1] # 4 newFile[1] # 4 有没有办法在Java中向自定义类添加类似

在Python中,可以添加列表下标作为自定义类的数据结构的访问器:

class customFile:
    # other methods ...
    def __getitem__(self, x):
        return self.list[x]
要获取的行为,请执行以下操作:

newFile = customFile()
newFile.list[1] = 4
newFile.list[1]
# 4
newFile[1]
# 4

有没有办法在Java中向自定义类添加类似的内容?

Java中没有直接的等价物。最接近的等效方法是提供您自己的集合类型实现。通常,您可以扩展
AbstractList
,并提供
get(int)
size()
方法,从而完全控制列表的内容和大小