Duplicate values cannot be added to JavaList via Add(index, item) #9675
Labels
Area: Mono.Android
Issues with the Android API binding (Mono.Android.dll).
Milestone
Android framework version
net9.0-android
Affected platform version
All Platforms/Versions
Description
When calling either
Add(Java.Lang.Object item)
orAdd(int index, Java.Lang.Object item)
on aJavaList
, duplicate values cannot be added to the list.Offending code:
android/src/Mono.Android/Android.Runtime/JavaList.cs
Lines 548 to 559 in dff6de5
I believe this is incorrect behavior for two reasons:
JavaList
is a wrapper over the native JavaArrayList
, and relevant Java documentation indicates thatAdd()
should always return true.JavaList
should match the expected behavior of the underlyingArrayList
,JavaList
itself is not internally consistent in applying this constraint. Any other way of adding or setting elements to aJavaList
does not reject duplicate elementsIt looks like it has been like this since at least 2016, and I wouldn't be surprised if there are consumers out there accidentally relying on this behavior. I recommend marking the two offending methods obsolete, and replacing them with correctly behaving methods with close-enough names like
AddItem
.Quick Edit: Example of this causing a real problem in the wild (and what brought it to my attention).
Steps to Reproduce
Use either
Add(Java.Lang.Object item)
orAdd(int index, Java.Lang.Object item)
to add duplicate values (e.g. "foo") repeatedly to aJavaList
.Did you find any workaround?
Adding or setting elements to
JavaList
through any other available method (e.g.Insert
,Set
,Add(JavaList collection)
allows duplicate values to be added.Relevant log output
None
The text was updated successfully, but these errors were encountered: