Is there any way to use Java similar to array and can use its custom classes?

Using Java, is there any way to make a custom class use [] accessors like arrays?

Normal array

int[] foo = int[5];
foo[4] = 5;
print(foo[4]);
//Output: "5"

Custom class

class Bar {
    //Custom class that uses index as a ref
}

Bar foo = new Bar(5);
foo.set(4,5);
print(foo[4]);
//Output: "5"

Solution

No, you can't overload the [] operator of your class with Java However, you can create getters for arrays

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>