I've tried searching for it, but I don't really now how to formulate the question correctly...
I have an if-statement
with many logical operators. How do I do this easier ?
If (n == 1 ||n == 2 ||n == 3 ||n == 5 ||n == 9 ||n == 8 ||n == 7 ||n == 551 ||n == 17 ||n == 81 || etc etc)
{ //Stuff
}
I'm thinking in pseudo-code I want something like this:
List list = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, or 36 }
if n is in list, then {}
As you understand, I'm a beginner and I have problems formulating what I need.
With if statements we often use the following logical operators: The logical AND operator ( && ) only returns true when the expression on its left and the one on its right are both true too. When the left, right, or both values are false , then && returns false too.
If you want to evaluate multiple logical tests within a single formula, then you can nest several functions one into another. Such functions are called nested IF functions. They prove particularly useful when you wish to return different values depending on the logical tests' results.
Type =IF( Excel will display the logical hint just below the cell F2. The parameters of this function are logical_test, value_if_true, value_if_false. The first parameter contains the condition to be matched. You can use multiple If and AND conditions combined in this logical test.
There is no limit to the number of && you use in a statement. So it will work with 4, 5, 100. It fails because some of the conditions are falsey.
How about:
int[] validOptions = { 1, 2, 3, 5, 7, 8, 9, 17, 81, 551 };
if (Arrays.binarySearch(validOptions, n) >= 0)
{
// Yup, found it
}
Note that I reordered your original check (which had 551 before 17 for example) so that the array is sorted. A binary search will only work with sorted data.
I'm only suggesting an array here on the grounds that it's slightly easier to specify, particularly as you're dealing with a primitive type. If you want these to be dynamic, a List<Integer>
or a HashSet<Integer>
would be more appropriate.
Note that while it almost certainly doesn't matter while the set is small, it's worth considering the performance characteristics of different lookups:
HashSet.contains
- O(1)Arrays.binarySearch
- O(log N)ArrayList.contains
- O(N)Yes, throw everything in an ArrayList
and then use the .contains(Object obj)
method:
List<Integer> numbers = new ArrayList<Integer>();
number.add(...;
if(numbers.contains(n))
{
...
}
Try this:
static private Set<Integer> set = new HashSet<Integer>(
Arrays.asList(1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36));
then to test:
if (set.contains(n))
Using a HashSet
will make it perform pretty well.
Your List approach is correct. You can do something like this:
int[] values = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 };
List<Integer> list = new ArrayList<Integer>();
if(values.asList(list).contains(n)) {
...
}
function arrayinside(n, listofn) {
var length = listofn.length;
for(var i = 0; i < length; i++) {
if(listofn[i] == n) return true;
}
return false;
}
Try this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With