Android Java How to remove int from a list correctly?
I am currently trying to write code that will create a list from different point coordinates and then remove the 3 smallest numbers from the list. When I run the application, it crashes. I figured out what I was doing in the remote part. I've looked at other similar threads, but the solution is similar to what I have. Here is the code I have:
List<Integer> XPoint = Arrays.asList(A.x, B.x, C.x, D.x, E.x, F.x, G.x, K.x);
List<Integer> XPLeft = Arrays.asList();
int XPLeftTimes = 0;
//Find 3 min X values(left)
while(XPLeftTimes != 2){
int Left = Collections.min(XPoint);
XPoint.remove(Left); <-App crashes here
XPLeft.add(Left);
XPLeftTimes++;
}
What am I doing wrong? Thanks in advance.
+3
source to share
3 answers
Arrays.asList () returns a fixed size array that cannot be changed. if you want to change it, you must create a new modifiable Arraylist by copying its contents like this:
List XPoint = new ArrayList(Arrays.asList(A.x, B.x, C.x, D.x, E.x, F.x, G.x, K.x));
XPLeft = new ArrayList ();
This should work.
0
source to share