خرید بک لینک

Vote count: -2

I'm currently writing a for loop to go through a 2D with 38 elements. However, some of those elements are null and I want the for loop to simply skip over them (because in the puzzle I'm solving they have no moves associated with them). A quick search revealed that I can skip an iteration with a simple continue statement in an if statement at the begiing of the for loop. However, I want to skip multiple iterations, and the if statement I wrote isn't working:

                for (int i = 0; i < triplets.length; i++) {
                if (i == 18 || 19 || 25 || 26 || 28 || 29 || 31 || 32) {
                    continue;
                }

Is there an alteative to making 8 individual if statements or am I stuck with that?

asked 6 mins ago

2 Answers

Vote count: 2

You are not giving the condition properly. It should be something like this:

if (i == 18 || i == 19 || i == 25 || i == 26 || i == 28 || i == 29 || i == 31 || i == 32) {
    /* Code */
}

Alteatively, I would suggest you to do something like this instead:

Set<Integer> set = new HashSet<>();
set.add(18);
set.add(19);
set.add(25);
set.add(26);
set.add(28);
set.add(29);
set.add(31);
set.add(32);

if(set.contains(i)) {
    continue;
} else {
    /* Code Here */
}

answered 3 mins ago

Vote count: -2

Is there an alteative to making 8 individual if statements or am I stuck with that?

List<Integer> exclude = Arrays.asList(18, 19, 25, 26, 28, 29, 31, 32);
for (int i = 0; i < triplets.length; i++) {
    if (exclude.contains(i)) {
        continue;
    }
}

answered 3 mins ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 11 فروردين 1395 ساعت: 23:54

صفحه بندی