0

I have a multi dimensional array as

private int numbers[][] ={{ 170, 100 },{ 270, 100 },{ 370, 100 },{ 470, 100 }}

Now i am changing there positions using Collections.shuffle(Arrays.asList(numbers)); Now i want to remove the the first item from the shuffled array i.e if the first item is {170,100} , it must be deleted from the array. For that, i tried to do something like this

 List<int[]> points =Arrays.asList(numbers);
 Collections.shuffle(points);
 points.remove(0);

but it is throwing java.lang.UnsupportedOperationException could anybody help me out to remove the first element from the two dimensional day.

4 Answers 4

3

Use following code:

 List<int[]> points =new ArrayList(Arrays.asList(numbers));
 Collections.shuffle(points);
 points.remove(0);
Sign up to request clarification or add additional context in comments.

Comments

2

How about

List<int[]> points = new ArrayList(Arrays.asList(numbers));

Comments

0

Well, its written in the API of Arrays#asList method

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

You cannot modify the returned list. What you can do is to employ Masud's suggestion.

Comments

0

The problem is that Arrays.asList returns a List which is privately declared in Arrays class.

 private static class ArrayList<E> extends AbstractList<E>
    implements RandomAccess, java.io.Serializable

and doesn't implement modification operations, thus yuo're getting

public E remove(int index) {
    throw new UnsupportedOperationException();
}

from it's superclass AbstractList.

As Reimeus suggested: wrap it into java.util.ArrayList or LinkedList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.