gi pi
gi pi

Reputation: 103

How can I delete an element of an ArrayList from another ArrayList in java?

I have two ArrayLists array1 and array2.

In both ArrayLists there can be different numbers, but in array2 it's always just one number. In an onClickListener I need the number of array2 to be deleted from array1.

For example:

ArrayList array1 = new Arraylist(2,1,4,6,3);
ArrayList array2 = new Arraylist(1);

Then I tried:

array1.remove(array2)

but nothing happens...

What is the best way to achieve that?

For Context:

I have three fragments, one where you can see your deck DeckKartenFragment and two where you can choose your card RadlerKarte and HopfentrunkKarte. I have two ImageViews, where you can put the chosen cards.

I want that each card can only be chosen once. For that I made two Arrays. One in which all chosen cards are deck.deck (Is stored in class deck) and one in which the current chosen card of the ImageView is deck1WelcheKarte.


//Check if card is already chosen// 
if (deck.deck.contains(1) == true){
                        Toast.makeText(getActivity(), "example" , Toast.LENGTH_LONG).show();
                        deck.cardchosen.clear();
                    }
if (deck.deck.contains(1) == false){
                        deck.deck.add(1);  //Add Card to deck//
                        deck.deck.removeAll(deck1WelcheKarte);  //Remove card that was chosen before from deck//
                        deck1WelcheKarte.clear(); //Reset card that is chosen to current card//
                        deck1WelcheKarte.add(1);}}

Now if I chose my first card everything works fine, when I replace that card with a new one it still works. But when i replace that card again with my first card, the number for that first card seems to be still in deck, because my app goes to makeToast instead of add new card to deck...

Upvotes: 0

Views: 437

Answers (3)

Marco Tizzano
Marco Tizzano

Reputation: 1896

Following the previous answer, which is a good one, you have to change the type of your java.uti.ArrayList because Java Collections don't support primitive types.
Actually, when you try to add a primitive type element into the ArrayList, the value gets implicitly wrapped by an Object class of its own type, for example
This statement array1.add(1); -> It's same as -> array1.add(new Integer(1));

Here is an example to let you understand:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        int myArray[] = { 2, 1, 4, 6, 3 };

        List<Integer> arrayList1 = new ArrayList();
        for (int elem : myArray) {
            arrayList1.add(elem);
        }

        List<Integer> arrayList2 = new ArrayList();
        arrayList2.add(2);

        System.out.println("array of int: " + Arrays.toString(myArray));
        System.out.println("arrayList2 (List of " + arrayList2.get(0).getClass().getName() + " ): " + arrayList2);

        System.out.println("Before remove - arrayList1: " + arrayList1);

        for (Object a : arrayList2) {
            System.out.println("Removing element (type " + a.getClass().getName() + "): " + a);
            arrayList1.remove(a);
        }

        System.out.println("After remove - arrayList1" + arrayList1);
    }
}

Output is:

array of int: [2, 1, 4, 6, 3]
arrayList2 (List of java.lang.Integer ): [2]
Before remove - arrayList1: [2, 1, 4, 6, 3]
Removing element (type java.lang.Integer): 2
After remove - arrayList1[1, 4, 6, 3]

As you see from the code, I just printed out the name of the class which is used when you add any int to the java.util.List of Object, and they actually get wrapped by java.lang.Integer class.

Upvotes: 1

Jakub D&#243;ka
Jakub D&#243;ka

Reputation: 2625

okey then try this code and copare it to yours:

public static void main(String[] args){
        ArrayList<Integer> a = new ArrayList<>(6);
        // ArrayList is kind of lacking right constructor so we have to insert values manually
        for(int i : new int[]{1, 2, 3, 4, 5, 6}) {
            a.add(i);
        }

        ArrayList<Integer> b = new ArrayList<>(1);
        b.add(1);

        // integer is important if we use int, it will remove by index
        for(Integer i : b) {
            a.remove(i);
        }

        System.out.println(a);
    }

Upvotes: 2

Jakub D&#243;ka
Jakub D&#243;ka

Reputation: 2625

for(int i : array2) {
    array1.remove(i)
}

this way you dont have to chack if array2 contains any element, it also covers multiple elements

Upvotes: 4

Related Questions