user3184419
user3184419

Reputation: 15

How to remove an item from a List<Integer> that is inside an array?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView t;
    List<Integer> listA = Arrays.asList(0,1,2,3);
    List<Integer> listB = Arrays.asList(0,2,4,6,8);
    List<Integer>[] listC = (List<Integer>[])new List[2];

    t = (TextView)findViewById(R.id.textView1);
    listC[0] = listA;
    listC[1] = listB;
    t.setText("Result: "+ listC[0].get(1)); //Result: 1
    listC[0].remove(0); //i get an error in this line
}

I am very confused as of why I don't get an error with: listC[0].get(1) but something's wrong with: listC[0].remove(0); Is there something wrong with my codes? Is there a more efficient way? Please help me! Thank you so much!

Upvotes: 0

Views: 138

Answers (3)

Sinto
Sinto

Reputation: 930

Instead of arrays I would suggest to use List<List<Integer>> listC = new ArrayList<List<Integer>>(2); a much cleaner way of doing it.

you can do all your operations like as follows.

 public static void main(String args[]) throws Exception {
        List<Integer> listA = Arrays.asList(0, 1, 2, 3);
        List<Integer> listB = Arrays.asList(0, 2, 4, 6, 8);
        List<List<Integer>> listC = new ArrayList<List<Integer>>(2);
        listC.add(listA);
        listC.add(listB);
        System.out.println("Complete List : " + listC);
        System.out.println("First Element :" + listC.get(0));
        listC.remove(1);// removed 2nd element
        System.out.println("List After Removal : " + listC);
    }

Output:

Complete List : [[0, 1, 2, 3], [0, 2, 4, 6, 8]]
First Element :[0, 1, 2, 3]
List After Removal : [[0, 1, 2, 3]]

Upvotes: 0

Eddy
Eddy

Reputation: 589

From doc for Arrays.asList :

List java.util.Arrays.asList(Integer... array)

public static List asList (T... array) Added in API level 1 Returns a List of the objects in the specified array. The size of the List cannot be modified, i.e. adding and removing are unsupported, but the elements can be set. Setting an element modifies the underlying array.

Parameters array the array.

Returns a List of the elements of the specified array.

You can not change the size of the list,it will be better to change

List<Integer> listA = Arrays.asList(0, 1, 2, 3);
List<Integer> listB = Arrays.asList(0, 2, 4, 6, 8);

TO

ArrayList<Integer> listA = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3));
ArrayList<Integer> listB = new ArrayList<Integer>(Arrays.asList(0, 2, 4, 6, 8));

Upvotes: 0

Gopal Gopi
Gopal Gopi

Reputation: 11131

probably you will get UnSupportedOperationException as Arrays.asList() is retuning unmodifiable List. so change

List<Integer> listA = Arrays.asList(0,1,2,3);
List<Integer> listB = Arrays.asList(0,2,4,6,8);

to

List<Integer> listA = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3));
List<Integer> listB = new ArrayList<Integer>(Arrays.asList(0, 2, 4, 6, 8));

Upvotes: 2

Related Questions