Why is my List code crashing

Question from Los Feliz#2763

is there a way to convert List<String> to ArrayList<Object>, and vice versa?

here's the full picture:

I have a String arr[].

I convert it into List<String> so I can show its content on Android app.

But then for performing filter for search purposes, I require an ArrayList of objects.

So I wanna convert my List<String> into an ArrayList<Object>, because my adapter is set to accept ArrayList as its params.

this is how i converted.

String arr[];
List&lt;String&gt; list1;

list1 = Arrays.asList(getResources().getStringArray(R.array.string-array's name));

thought i know what i'm doing until i saw the app crash with what i have now.

Unknown bits set in runtime_flags: 0x8000
E/libc: Access denied finding property &quot;ro.serialno&quot;
E/AndroidRuntime: FATAL EXCEPTION: main

at java.util.AbstractList.remove(AbstractList.java:161)
at java.util.AbstractList$Itr.remove(AbstractList.java:374)
at java.util.AbstractList.removeRange(AbstractList.java:571)
at java.util.AbstractList.clear(AbstractList.java:234)
at com.example.cspeaks.MyAdapter$2.publishResults(MyAdapter.java:105)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:284)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
```.

ohhh. wrap your Arrays.asList(...) with new ArrayList<>(Arrays.asList(...)) that might help. IDK though that message is horrific.

OMGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG @emccue YOU ARE THE MAN! JESUS CHRIST! THANK YOUUUUUUUUUUUUUUUUUUUUU

Okay don't leave yet. I want you to understand why that helped.

List is an interface that has methods that read from a list (like .get) and methods that alter a list (like .remove).

An implementation of that interface like ArrayList supports all the methods. You can get elements, add elements, and remove elements.

But there are a bunch of methods in the JDK that return lists that are unmodifiable. Arrays.asList is one of those, because the purpose of it is to be a list "view" onto an array.

ooooooo. So in other words, when we have Arryas.asList, if we want to make changes to the array, we must change it to arraylist

That doesn't parse for me, Arrays are their own things.

So if you have a String[], that is a fixed size collection. It does not implement the List interface.

Arrays.asList(..) is a way you can take aString[] and get a List<String>, but the List you get doesn't support adding or removing elements.

If you want to add or remove elements from that list you need to copy its contents into a list that supports that.

OMG. i learned this hard way. Almost 20 hours.

ArrayList is the most common one and a good default. List.of(...) and .toList() on a Stream also give you unmodifiable lists - though those ones don't support setting elements in place either.

Its annoying, but you just need to be aware of it. If you are in doubt, copy it into an ArrayList and you know you can do everything.

yeah. lesson learned. from now on Im coppying every instance of List<> into an arraylist

Thats one approach.

In other contexts that can be called "defensive copying" - Its a generally good technique if you are working with mutable structures though overkill if you are the one making and working with all the instances (potentially).


<- Index