如何判断一个数组中是否包含一个字符或字符串
1个回答
2017-07-31
展开全部
第一种方法:遍历数组
String[] arr1 = {"1","2","3","4","6","7"};
for (int i = 0; i < arr1.length; i++) {
if("5".equals(arr1[i])) {
System.out.println("包含");
}else {
System.out.println("不包含");
}
}
第二种方法:先把数组转换成list;再判断contains
String[] arr = {"1","2","3","4","6","7"};
List<String> list=Arrays.asList(arr);
System.out.println(list.contains("5"));
String[] arr1 = {"1","2","3","4","6","7"};
for (int i = 0; i < arr1.length; i++) {
if("5".equals(arr1[i])) {
System.out.println("包含");
}else {
System.out.println("不包含");
}
}
第二种方法:先把数组转换成list;再判断contains
String[] arr = {"1","2","3","4","6","7"};
List<String> list=Arrays.asList(arr);
System.out.println(list.contains("5"));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询