0

I am making an app for android. I am trying to guess what song the user just played on xylophone. I have: private List<String> mDespacito = new ArrayList<String>(Arrays.asList("f", "a","d","d")); and private List<String> mPlayed = new ArrayList<String>(); When user presses one key on the xylophone, I add the key he pressed to the mPlayed arraylist, like this:

public void playD(View v){
    Log.d("Xylophone", "Played D!");
  mSoundPool.play(mDSoundId,LEFT_VOLUME,RIGHT_VOLUME,PRIORITY,NO_LOOP,NORMAL_PLAY_RATE);
    mPlayed.add("d");
    CheckForSong();

}

Now, CheckForSong contains:

public void CheckForSong(){
    if (mPlayed.containsAll(mDespacito)){
        Log.d("Xylophone","You just played despacito");
        mPlayed.removeAll(mDespacito);
    }
}

So, it should do:

played F
played A
played D
played D
You just played despacito

But it does:

played F
played A
played D
You just played despacito
played D

And you can do even:

played F
played G
played A
played G
played D
You just played despacito

And I know why: Because of if (mPlayed.containsAll(mDespacito)) just checks if elements of mDespacito are in mPlayed. But i need to check if there are all elements of mDespacito (Including those which are there twice) and if they are in the right order. Is there any command like that I can use? Thanks

Vítek Peterka
  • 89
  • 1
  • 2
  • 10

3 Answers3

1

Use

mPlayed.equals(mDespacito);

instead, so the elements will be checked in order and elements.

IMPORTANT: If you are not using Strings as your code demonstrate, you need to implement hashCode and equals in the class that you add to the list.

The following snippet results in displaying true twice then false

import java.util.ArrayList;

public class MyClass {
    public static void main(String args[]) {
        ArrayList<String> a = new ArrayList();
        a.add("f");
        a.add("a");
        a.add("d");
        a.add("d");

        ArrayList<String> b = new ArrayList();
        b.add("f");
        b.add("a");
        b.add("d");
        b.add("d");

        System.out.println(a.equals(b));
        System.out.println(b.equals(a));
        b.add("c");
        System.out.println(a.equals(b));
    }
}

Else: You can compare the list yourself:

 public boolean equals(List f, List s) {
      if(f.size() != s.size())
         return false;
      for(int i = 0; i < f.size(); i++)
          if(!f.get(i).equals(s.get(i))
             return false;
 }

But remember the tip that if you are not using primitives or String you need to implement hashCode and equals on your objects.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • hmm, no that does not work, too. This time it does not even show You just played despacito. I think it is because of mPlayed does not equal mDespacito, it just contains mDespacito – Vítek Peterka Oct 19 '17 at 12:15
1

Collections.indexOfSubList is the answer. I use it like this:

public void CheckForSong(){
    int contains = Collections.indexOfSubList(mPlayed, mDespacito);
    if (contains != -1){
        Log.d("Xylophone","You just played despacito");
        mPlayed.removeAll(mDespacito);
    }
}

more about Collections.indexOfSubList: https://www.tutorialspoint.com/java/util/collections_indexofsublist.htm

Vítek Peterka
  • 89
  • 1
  • 2
  • 10
0

list1.equals(list2) can be used

A very nice explanation is given in the below link. Please find.

https://stackoverflow.com/a/1075699/4994582

  • hmm, no that does not work, too. This time it does not even show You just played despacito. I think it is because of mPlayed does not equal mDespacito, it just contains mDespacito – Vítek Peterka Oct 19 '17 at 12:16