我提出的Android应用程序的其他一个ArrayList的一部分。我试图猜测用户刚刚在木琴上演奏了哪首歌。我有:private List<String> mDespacito = new ArrayList<String>(Arrays.asList("f", "a","d","d"));
和private List<String> mPlayed = new ArrayList<String>();
当用户按下木琴一个关键,我加他压到mPlayed ArrayList中的关键,是这样的:Java是为了
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();
}
现在,CheckForSong包含:
public void CheckForSong(){
if (mPlayed.containsAll(mDespacito)){
Log.d("Xylophone","You just played despacito");
mPlayed.removeAll(mDespacito);
}
}
所以,它应该做的:
played F
played A
played D
played D
You just played despacito
但它的作用:
played F
played A
played D
You just played despacito
played D
你甚至可以这样做:
played F
played G
played A
played G
played D
You just played despacito
而且我知道为什么:因为if (mPlayed.containsAll(mDespacito))
的只是检查是否mDespacito的元素是mPlayed。但我需要检查是否有mDespacito的所有元素(包括那些有两次),如果他们在正确的顺序。有没有像我可以使用的命令?由于
'Collections.indexOfSubList'可以帮助你! –
如果处理个人笔记,这将得到串连成一个字符串中的字符,你可以使用'String.contains()'这需要顺序考虑。 –