Java - custom getter array
Does Java have the ability to C # syntax equal
class MyClass{
private int[] array = new int[20];
public int this[int index] { get{ return array[i];}} //<-- array getter for object
}
MyClass test = new MyClass();
Console.WriteLine(test[0]);
(The code is just an example;))
+3
Vectro
source
to share
2 answers
Java does not support operator overloading, including the array subscript operator ( []
).
+7
Mureinik
source
to share
No, you cannot override / overload operators - Java does not support this. However, you can add a get method like:
class MyClass{
private int[] array = new int[20];
public int get(int i) { return array[i]; }
}
+5
MrTux
source
to share