Java 一个数组列表是另一个数组列表的一部分吗
Java Is one arraylist part of another in order
我正在为 android 制作应用程序。我试图猜测用户刚刚在木琴上播放的歌曲。我有: private List<String> mDespacito = new ArrayList<String>(Arrays.asList("f", "a","d","d"));
和 private List<String> mPlayed = new ArrayList<String>();
当用户按下木琴上的一个键时,我将他按下的键添加到 mPlayed 数组列表中,如下所示:
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 的所有元素(包括那些出现两次的元素)以及它们的顺序是否正确。有没有我可以使用的命令?谢谢
使用
mPlayed.equals(mDespacito);
相反,因此将按顺序检查元素。
重要提示:如果您没有像代码演示的那样使用字符串,则需要在添加到列表的 class 中实现 hashCode 和 equals。
以下代码片段导致两次显示 true 然后显示 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));
}
}
其他:大家可以自己对比榜单:
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;
}
但请记住提示,如果您不使用原语或字符串,则需要在您的对象上实现 hashCode 和 equals。
list1.equals(list2)
可以用
下面link给出了一个很好的解释。请找。
Collections.indexOfSubList
就是答案。我这样使用它:
public void CheckForSong(){
int contains = Collections.indexOfSubList(mPlayed, mDespacito);
if (contains != -1){
Log.d("Xylophone","You just played despacito");
mPlayed.removeAll(mDespacito);
}
}
关于 Collections.indexOfSubList
的更多信息:
https://www.tutorialspoint.com/java/util/collections_indexofsublist.htm
我正在为 android 制作应用程序。我试图猜测用户刚刚在木琴上播放的歌曲。我有: private List<String> mDespacito = new ArrayList<String>(Arrays.asList("f", "a","d","d"));
和 private List<String> mPlayed = new ArrayList<String>();
当用户按下木琴上的一个键时,我将他按下的键添加到 mPlayed 数组列表中,如下所示:
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 的所有元素(包括那些出现两次的元素)以及它们的顺序是否正确。有没有我可以使用的命令?谢谢
使用
mPlayed.equals(mDespacito);
相反,因此将按顺序检查元素。
重要提示:如果您没有像代码演示的那样使用字符串,则需要在添加到列表的 class 中实现 hashCode 和 equals。
以下代码片段导致两次显示 true 然后显示 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));
}
}
其他:大家可以自己对比榜单:
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;
}
但请记住提示,如果您不使用原语或字符串,则需要在您的对象上实现 hashCode 和 equals。
list1.equals(list2)
可以用
下面link给出了一个很好的解释。请找。
Collections.indexOfSubList
就是答案。我这样使用它:
public void CheckForSong(){
int contains = Collections.indexOfSubList(mPlayed, mDespacito);
if (contains != -1){
Log.d("Xylophone","You just played despacito");
mPlayed.removeAll(mDespacito);
}
}
关于 Collections.indexOfSubList
的更多信息:
https://www.tutorialspoint.com/java/util/collections_indexofsublist.htm