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"
Answer: Yes. Java can have an array of objects just like how it can have an array of primitive types.
No, you cannot overload []
operator for your classes in Java
. But you can create getter for your array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With