With Java, is there a way to make a custom class that can have the [] accessor used on it like an array?
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"
No, you cannot overload
[]operator for your classes inJava. But you can create getter for your array.