3

Does Java have any possibility of c# equal syntax

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]);

( Code is just example ;) )

Vectro
  • 135
  • 2
  • 9

2 Answers2

7

Java does not support operator overloading, including the array subscript ([]) operator.

Mureinik
  • 277,661
  • 50
  • 283
  • 320
5

No, you cannot override/overload operators - Java doesn't support that. However, you can add a get method like:

class MyClass{
    private int[] array = new int[20];
    public int get(int i) { return array[i]; }
}
MrTux
  • 30,335
  • 25
  • 102
  • 137