public class Test {Output
public static void main(String[] args) {
int T[]={1, 5, 2, 22, 14, 3, 18, 52, 40, 88, 73, 27};
int valrech=22;
int position = exists(T, valrech);
if(position!=-1)
System.out.println(valrech+" is in the "+position" position);
}
static int exists(int T[], int val){
for(int i = 0 ; i< T.length; i++){
if(val==T[i])
//return current position
return i;
}
System.out.println("The value you are looking for does not exist");
return -1;
}
}
22 is in the 3
import java.util.Arrays;Output
public class Test {
public static void main(String[] args) {
int T[]={100,150,180,220,240};
System.out.println(contains(T, "220"));
}
static public boolean contains(int[] T, String val) {
return Arrays.toString(T).contains(val);
}
}
trueWhy contains() supports the String type but not the int type? Simply because int is a primitive type and String becomes an object after instantiation and can be used in contains(Object o) which only looks for objects.
import java.util.Arrays;Output
public class Test {
public static void main(String[] args) {
int T[]={10,14,28,201,280};
Integer[] T2 = {10,14,28,201,280};
//array int
System.out.println(Arrays.asList(T).contains(28));
//array Integer
System.out.println(Arrays.asList(T2).contains(28));
}
}
false
true
import java.util.Arrays;Output:
public class Test {
public static void main(String[] args) {
int T[]={'a','b','c','d','e','f'};
System.out.println("The value you are looking for is in
the position "+Arrays.binarySearch(T,'d'));
}
}
The value you are looking for is in the 3
Please disable your ad blocker and refresh the window to use this website.