如何高效檢查乙個陣列中是否包含某個值

2021-07-07 04:24:18 字數 505 閱讀 6043

1.檢查陣列中是否包含特定值的四種不同方法

1)使用list:

public static boolean uselist(string arr, string targetvalue)

2)使用set:

public static boolean useset(string arr, string targetvalue)

3)使用乙個簡單迴圈:

public static boolean useloop(string arr, string targetvalue)

return false;

}4)使用arrays.binarysearch():

注:下面的**是錯誤的,這樣寫出來僅僅為了理解方便。binarysearch()只能用於已排好序的陣列中。所以,你會發現下面結果很奇怪。

public static boolean usearraysbinarysearch(string arr, string targetvalue)

判斷乙個陣列是否包含另乙個陣列

function iscontainarr parent,child let parent 1,2,3,6,5,4 let child 1,3,4,6 let child2 1,3,4,6,7 console.log iscontainarr parent,child true console.lo...

Java 如何判斷乙個陣列中是否包含某個元素

public static boolean uselist string arr,string targetvalue public static boolean useset string arr,string targetvalue public static boolean useloop s...

WebDriver 中如何檢查乙個元素是否存在?

起因 最近自做乙個ui自動化註冊的事情,在使用 webdriver 的過程中遇到了頁面元素可能不存在的問題,因為元素不存在,再執行元素的相關操作的時候就會超時等待和丟擲異常,所以為了處理不存在,就是用了 try catch 的方式處理了一下,如果 try 中的不存在,那就執行 catch 中的 邏輯...