Arrays.asList(yourArray).contains(yourValue)
Warning: this doesn't work for arrays of primitives (see the comments).
Since java-8 you can now use Streams.
String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);
To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.
Example
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
Answer from camickr on Stack OverflowArrays.asList(yourArray).contains(yourValue)
Warning: this doesn't work for arrays of primitives (see the comments).
Since java-8 you can now use Streams.
String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);
To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.
Example
int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
Concise update for Java SE 9
Reference arrays are bad. For this case we are after a set. Since Java SE 9 we have Set.of.
private static final Set<String> VALUES = Set.of(
"AB","BC","CD","AE"
);
"Given String s, is there a good way of testing whether VALUES contains s?"
VALUES.contains(s)
O(1).
The right type, immutable, O(1) and concise. Beautiful.*
Original answer details
Just to clear the code up to start with. We have (corrected):
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
This is a mutable static which FindBugs will tell you is very naughty. Do not modify statics and do not allow other code to do so also. At an absolute minimum, the field should be private:
private static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
(Note, you can actually drop the new String[]; bit.)
Reference arrays are still bad and we want a set:
private static final Set<String> VALUES = new HashSet<String>(Arrays.asList(
new String[] {"AB","BC","CD","AE"}
));
(Paranoid people, such as myself, may feel more at ease if this was wrapped in Collections.unmodifiableSet - it could then even be made public.)
(*To be a little more on brand, the collections API is predictably still missing immutable collection types and the syntax is still far too verbose, for my tastes.)
java - Check if a value exists in an array - Stack Overflow
How to check if a value exists in an array in Java? - Stack Overflow
Java check if array[] item exists - Stack Overflow
[Java] is there a method to check if an array contains a certain value?
One way to do it is using a for loop:
//conventional for loop
for (int i = 0; i < arraynumbers.length; i++)
{
if (arraynumbers[i] == num)
System.out.print(num + " found!");
}
//"for each" loop
for (int number : arraynumbers)
{
if (number == num)
System.out.print(num + " found!");
}
What would you do, programming set aside, with a paper and a pen, to perform that operation manually? Considering that the list can be much bigger than 10 elements, you need a systematic approach.
You would probably look at each element of the list, one after the other, and compare that element with the number you are looking for.
Looking at each element of a list is what a loop does.
Testing and comparing usually involves relational operators, like ==, or <
So, you need to loop through all the elements, compare them with the value you are looking for using the right operator, and test the result of that comparison:
for (int value: arraynumbers) {
if (value == num) {
// Here you found the number you are looking for
}
}
Arrays.asList is a generic function that takes a parameter of T... array, in case of int[] the only applicable type is int[], i.e. your list will contain only one element, which is the array if integers. To fix it use boxed primitive types:
Integer[] portOut = {4000,4001,4002,4003,4004,4005,4006,4007,4008,4009};
Integer[] result = {4001, 4005, 4003, 0, 0, 0, 0, 0, 0, 0};
Just write two for loops and check whether any given element in one array is in the other. So just:
for (int i=0; i< portOut.length; i++){
for(int j=0;j<result.length;j++) {
//rest of code
}
}
To prevent the ArrayIndexOutOfBoundsException, you can use the following:
if(ImageLinks.length > 5 && ImageLinks[5] != null)
{
// do something
}
As the statements in the if are checked from left to right, you won't reach the null check if the array doesn't have the correct size.
It's quite easy to generalise for any scenario.
Write a static function
public static boolean indexInBound(String[] data, int index){
return data != null && index >= 0 && index < data.length;
}
Now, give it a call in your code
if(indexInBound(ImageLinks, 5) && ImageLinks[5] != null){
//Your Code
}