简体   繁体   中英

How to remove an element from the two dimensional array in java

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 ie 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.

Use following code:

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

怎么样

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM