[英]how to check that specific vector exist in list of vectors in java?
我有位矢量(0和1)的列表,我想檢查該列表中是否存在特定的位矢量。 我使用了此代碼,但答案不正確。
if(bitset2.contains(bi))
System.out.println("data found in file");
else
System.out.println("data not found in files ");
bitset2
代表位向量列表, bi
是位向量。
盡管矢量存在於列表中並且條件已得到驗證,但是此代碼始終會打印data not found in files
。
您可以循環拋出List並使用.equals
來檢查Vector是否存在於List中,例如:
public static void main(String[] args) {
Vector vec1 = new Vector(3);
vec1.add(0, 0);
vec1.add(1, 1);
vec1.add(2, 0);
Vector vec2 = new Vector(3);
vec2.add(0, 0);
vec2.add(1, 1);
vec2.add(2, 0);
//create a List of Vectors
List<Vector> bitset2 = new ArrayList<>();
bitset2.add(vec2);
boolean exist = false;
//Loop throw your List and check if your vector exist or no
for (Vector vector : bitset2) {
if (vector.equals(vec1)) {
exist = true;
break;
}
}
System.out.println(exist ? "data found in file" : "data not found in file");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.