我正在制作一个Android应用程序,根据规格选择产品,并发送带有产品代码的短信。 我有一个庞大的产品列表与diff规格。 我需要比较一下。 23个规格选择产品。 我知道我可以使用多个if语句或嵌套ifs,但我想知道是否有更好的方法来实现相同。只有当所有23个规格完全相同时,我才能选择产品。 所有规格都存储在M1到M49之类的变量中,并根据产品代码进行映射。
短信部分已经完成,但我很难比较这么多变数。
任何指针都受到高度赞赏。
答案 0 :(得分:1)
您可以使用JAVA的retainAll功能
class Check
{
static String s[] = { "q","w","e","r","t","y","u","i","o","p","a",
"mmmmm","d","f","g","h","j","k","l","z","x","c","v" };
static String s1[] = {"b","n","m","er","re","ew","j","k","x","qq","ww","ee","c","v","t","y","d","f","g","h",
"u","rr","yyy","uuu","ttt","trew","rerer","q","w","vv","gg","hh","tt","tr","ww","e","i"
,"eww","qwe","wer","o","p","a","s","ds","cv","r","l","z"};
public static void main(String arags[])
{
Set<String> originalset = new HashSet<String>(Arrays.asList(s1));
Set<String> testset = new HashSet<String>(Arrays.asList(s));
originalset.retainAll(testset);
if(originalset.size()==23)
{
System.out.println("Sucess 23 matches found");
}
else
{
System.out.println("Fail 23 matches not found");
}
}
}
答案 1 :(得分:0)
步骤:
1. Put both of them into a HashSet. A HashSet searches faster and doesn't allow duplicate elements.
2. Find if first hashset is a subset of second hashset.
import java.util.Iterator;
public class Sort {
public static void main(String[] args) {
String s[] = { "q","q","q","r","t","y","u","i","o","p","a",
"m","d","f","g","h","j","k","l","z","x","c","v" };
String s1[] = {"b","n","m","q","q","er","re","ew","j","k","x","qq","ww","ee","c","v","t","y","d","f","g","h",
"u","rr","yyy","uuu","ttt","trew","rerer","q","w","vv","gg","hh","tt","tr","ww","e","i"
,"eww","qwe","wer","o","p","a","s","ds","cv","r","l","z"};
/* System.out.println("Length of s : " + s.length);
System.out.println("Length of s1 : " + s1.length);
Arrays.sort(s);
Arrays.sort(s1);
display(s);
display(s1);
int j=0;
int count=0;
for(int i=0;i<s1.length;i++) {
if(s[j].equals(s1[i])) {
count++;
j++;
if(j==23) break;
}
}
if(j==23) System.out.println("23 matches found");
else System.out.println("23 matches not found");
*/
HashSet<String> hs1 = createSet(s);
HashSet<String> hs2 = createSet(s1);
System.out.println(h1SubsetOfh2(hs1, hs2));
}
public static HashSet<String> createSet(String[] sArr) {
HashSet<String> hs = new HashSet<String>();
for(int i=0;i<sArr.length;i++) {
hs.add(sArr[i]);
}
return hs;
}
public static boolean h1SubsetOfh2(HashSet<String> hs1,HashSet<String> hs2) {
Iterator<String> it = hs1.iterator();
while(it.hasNext()) {
if(!hs2.contains(it.next()))
return false;
}
return true;
}
}
答案 2 :(得分:0)
import java.util.Arrays;
public class Sort {
public static void main(String[] args) {
String s[] = { "q","w","e","r","t","y","u","i","o","p","a",
"mmmmm","d","f","g","h","j","k","l","z","x","c","v" };
String s1[] = {"b","n","m","er","re","ew","j","k","x","qq","ww","ee","c","v","t","y","d","f","g","h",
"u","rr","yyy","uuu","ttt","trew","rerer","q","w","vv","gg","hh","tt","tr","ww","e","i"
,"eww","qwe","wer","o","p","a","s","ds","cv","r","l","z"};
System.out.println("Length of s : " + s.length);
System.out.println("Length of s1 : " + s1.length);
Arrays.sort(s);
Arrays.sort(s1);
display(s);
display(s1);
int j=0;
int count=0;
for(int i=0;i<s1.length;i++) {
if(s[j].equals(s1[i])) {
count++;
j++;
if(j==23) break;
}
}
if(j==23) System.out.println("23 matches found");
else System.out.println("23 matches not found");
}
public static void display(String a[]) {
for(int i=0;i<a.length;i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
试试这个。