導航:首頁 > 編程語言 > java判斷數組包含

java判斷數組包含

發布時間:2022-03-13 02:01:41

java中怎麼判斷一個字元串數組中包含某個字元或字元串

packagetest;

publicclassTest{

publicstaticvoidmain(String[]args){

Stringstr="ab";

System.out.println(isStr(str).toString());

}


/**

*判斷一個字元串數組中包含某個字元或字元串:返回一個boolean:參數判斷的字元

*1.定義一個字元串數組

*2.遍歷這個數組

*3.判斷要指定的字元串是否包含在字元串數組內

*4.如果包含返回true,否則返回false

*/

publicstaticBooleanisStr(Stringstr){

Stringarray[]={"a","b","c","hello"};

for(inti=0;i<array.length;i++){

if(str.equals(array[i])){

returntrue;

}

}

returnfalse;

}

}

❷ 在java中怎麼判斷一個數組包含另一個數組

雙循環,把數組中的每一個值都拿來做比較、判斷!!1

public static boolean containArray(int[] a, int[] b) {

boolean flag = false;

int k = 0;

/**

* 統計b中包含a中的元素是否與a的元素個數相同

*/

if (a.length < b.length) {

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < b.length; j++) {

if (a[i] == b[j]) {

k++;

System.out.println(a[i] +"/"+ b[j]);

continue;

}

}

}

}

if (k == a.length) flag = true;

return flag;

}

❸ 在Java中判斷數組中包含某個元素的幾種方式的比較

直接上代碼:
package test.contain.lishaojie;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class TestContain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr = new String[] { "DD", "CC", "DD", "FF", "KK"};
String target ="A";
int times = 1000;//次數
//轉換成list方式
long startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByList(arr, target);
}
long endTime = System.currentTimeMillis();
long ration = endTime - startTime;
System.out.println("list方式: " + ration / 1000000);

//轉換成set方式
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
BySet(arr, target);
}
endTime = System.currentTimeMillis();
ration = endTime - startTime;
System.out.println("set方式: " + ration / 1000000);

//直接循環方式
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByForLoop(arr, target);
}
endTime = System.currentTimeMillis();
ration = endTime - startTime;
System.out.println("循環方式: " + ration / 1000000);

//二分法查找
startTime = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
ByArraysBinarySearch(arr, target);
}
endTime = System.currentTimeMillis();
ration = endTime - startTime;
System.out.println("二分法查找: " + ration / 1000000);
}
public static boolean ByList(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
public static boolean BySet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}
public static boolean ByForLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}
public static boolean ByArraysBinarySearch(String[] arr, String targetValue) {
int a = Arrays.binarySearch(arr, targetValue);
if(a > 0)
return true;
else
return false;
}
}
運行結果如下:
list方式: 5
set方式: 22
循環方式: 2
二分法查找: 3
經過大量數據測試循環方式效率最高,其次是二分法,最後是list,和set因為因為將數組壓入Collection類型中,首先要將數組元素遍歷一遍,然後再使用集合類做其他操作。但是list方式明顯要比set方式快很多,這是為什麼呢?直接看代碼:首先
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
返回的是ArrayList所以set方式還要進行一次操作將ArrayList轉換成set,
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
之一addAll方法:
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
又一次進行了選環,所以效率比較低,binggo

❹ java判斷一個數組是否含有該數字

//循環出每個數組,並且與該數字匹配,看是否相等!

列如:
int a=0;
int[] str;
str= new int[5];

for(int 1=0;i>str.length();i++){
if(str[i]==a){
system.out.print("該數組含有a這個數字");

}

}

❺ Java中數組是否包含某些元素

有兩種方法可以判斷數組是否包含元素:

方法1, 將數組轉換為list,然後使用list的contains方法來判斷:

Arrays.asList(...).contains(...)

方法2,遍歷數組判斷:

❻ java判斷一個數組是否含有該數字

可以把數組轉成list,利用list的contains方法來判斷
Integer[]arr=newInteger[]{1,2,3,4};

List<Integer>list=Arrays.asList(arr);

if(list.contains(5)){

System.out.println("包含");

}else{

System.out.println("不包含");

}

❼ java判斷數組中是否包含


importjava.util.Arrays;

publicclassTest_Array{

publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub

int[]test={1,6,3,8,9,2,5};
Arrays.sort(test);//首先對數組排序
intresult=Arrays.binarySearch(test,5);//在數組中搜索是否含有5
System.out.println(result);//這里的結果是3

}
}

上面是一種辦法 當然辦法還多 比如你還可以把它 轉成 list容器 然後調用 contains方法

甚至你可以自己封裝個方法 循環一遍...

❽ java如何判斷數組中是否包含某元素

這個問題,首先要將string數組轉換成list集合,然後判斷list集合中是否存在
public static void main(String[] args) {
//定義數組
String aa []={"timo","kainan","naer","lanbo"};
//數組轉換成list
List<String> list=Arrays.asList(aa);
if(list.contains("timo")){
System.out.println("有提莫");
}else{
System.out.println("沒有提莫");
}
}

❾ java中怎麼判斷一個字元串數組中包含某個字元或字

可以用contains()這個方法,判斷一個字元串是否包含另一個字元串。
可以用repalce()這個方法,把想刪除的字元替換成空。

public class Test {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "1111abcdefe";
if(s2.contains(s1)){
System.out.println("s2包含了s1");
//刪掉s1
s2 = s2.replace(s2.substring(s2.indexOf(s1), s2.length()), "");
System.out.println(s2);
}else{
System.out.println("s2不包含s1");
}
}
}

❿ 在Java中,如何檢測一個數組中是否包含某一個數據

在Java中,檢測一個數組是否包含某一個數據,通常有四種方法:

(1)for循環

(2)轉換為List,調用Arrays.asList(arr).contains方法

(3)使用Set

(4)使用Arrays.binarySearch()方法

下面為上述四種方法的具體代碼實現:

1、使用for循環

public static boolean useLoop(String[] arr, String targetValue) {

for (String s : arr) {

if (s.equals(targetValue))

return true;

}

return false;

}

閱讀全文

與java判斷數組包含相關的資料

熱點內容
架設傳奇命令 瀏覽:951
關於醫生的小說 瀏覽:518
愛情動作電影 瀏覽:808
八零電子書txt免費下載網站 瀏覽:509
登陸遼事通顯示伺服器連接錯誤怎麼辦 瀏覽:547
9米高隧道演算法 瀏覽:508
池袋最強作品集txt 瀏覽:782
app專題推薦在哪裡 瀏覽:277
神雲伺服器顯示燈 瀏覽:134
程序員磨合期技巧 瀏覽:847
鬼團六全部電影名稱 瀏覽:864
穿越唯一一個女人世界 瀏覽:645
飛言情小說官網入口 瀏覽:581
pdf壓縮後還清晰嗎 瀏覽:654
得到app的電子書書架在哪裡 瀏覽:151
管道彎頭製作演算法 瀏覽:37
phpmvcsmarty實例 瀏覽:925
spring搭建http伺服器地址 瀏覽:713
servlet教程pdf 瀏覽:970
蜂鳥眾包app如何聯系客服 瀏覽:188