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++;
    }

      

enter image description here

What am I doing wrong? Thanks in advance.

+3


source to share


3 answers


Arrays.asList () returns a fixed size list supported by the specified array.

try



List<Integer> xPoint = new ArrayList(Arrays.asList(A.x, B.x, C.x, D.x, E.x, F.x, G.x, K.x));

      

+2


source


When you call XPoint.remove(left);

it does not remove that part, it removes any value stored in the index (which is on the left) why is it failing If you want to remove this number try this



XPoint.remove(new Integer(left));

      

+1


source


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







All Articles