Reputation: 1478
Currently I am trying to write a code that will create a list from various points coordinates and then remove 3 smallest ints from the list. When I run the app, it crashes. I figured out that i happens in remove part. I have looked into other similar threads, but the solution are similar to what I have. Here's 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.
Upvotes: 3
Views: 464
Reputation: 1566
Arrays.asList() returns a fixed-size list backed 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));
Upvotes: 2
Reputation: 1399
Arrays.asList() returns fixed size array which cannot be modified. if you want to modify it, you have to create a new modifiable Arraylist by copying its contents as follows:
List XPoint = new ArrayList(Arrays.asList(A.x, B.x, C.x, D.x, E.x, F.x, G.x, K.x));
List XPLeft = new ArrayList();
This should work.
Upvotes: 0
Reputation: 4375
When you are calling XPoint.remove(left);
it is not removing that part it is removing whatever value is stored at the index(which is equal to left) that's why it is crashing If you want to remove that number try this
XPoint.remove(new Integer(left));
Upvotes: 1