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
  • 297,002
  • 52
  • 306
  • 350
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
  • 32,350
  • 30
  • 109
  • 146