java判断一个数组是否含有该数字
可以把数组转成list,利用list的contains方法来判断
Integer[] arr = new Integer[] { 1, 2, 3, 4 };
List<Integer> list = Arrays.asList(arr);
if (list.contains(5)) {
System.out.println("包含");
} else {
System.out.println("不包含");
}
public class Test {
public static void main(String[] args) {
boolean flag = false;
int[] is = new int[1000];
for (int i = 0; i < is.length; i++) {
is[i] = i;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数");
int num = scanner.nextInt();
for (int i : is) {
if (num == i) {
flag = true;
}
}
if (flag) {
System.out.println("有这个整数");
} else {
System.out.println("没有这个整数");
}
}
}
可以把数组转成list,利用list的contains方法来判断
Integer[] arr = new Integer[] { 1, 2, 3, 4 };
List<Integer> list = Arrays.asList(arr);
if (list.contains(5)) {
System.out.println("包含");
} else {
System.out.println("不包含");
}
public class Test {
public static void main(String[] args) {
boolean flag = false;
int[] is = new int[1000];
for (int i = 0; i < is.length; i++) {
is[i] = i;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数");
int num = scanner.nextInt();
for (int i : is) {
if (num == i) {
flag = true;
}
}
if (flag) {
System.out.println("有这个整数");
} else {
System.out.println("没有这个整数");
}
}
}
列如:
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这个数字");
}
}