I want to do this in Java. Is it possible?
public string this[int pos]
{
get
{
return myData[pos];
}
set
{
myData[pos] = value;
}
}
No. You can't overload any operators in Java, including indexing. (String overloads +, but that's baked into the language specification.)
Only arrays support [] syntax.
You'd generally write methods instead:
public String getValue(int position) {
return myData[position];
}
public void setValue(int position, String value) {
myData[position] = value;
}
No, Java does not have anything similar to C#'s indexers or overloaded operators. That is the most likely reason why the function call syntax is used in String.charAt, List.get, Map, put, and so on.
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