0
for (int i = 0; i < numArray.size(); i++) {
    if (numArray.get(i) % 2 == 0) {
        evenSum += numArray.get(i);
        outputArea.append(numArray.get(i) + "\n");
    }
}

Is there any way to get the line

if (numArray.get(i) % 2 == 0) {

in the for loop condition line? And if so, would that be more efficient than how it is right now?

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Alice
  • 29
  • 3

2 Answers2

2

You could put the condition in the for loop:

for (int i = 0; (i < numArray.size()) && (numArray.get(i) % 2 == 0); i++) {
    evenSum += numArray.get(i);
    outputArea.append(numArray.get(i) + "\n");
}

However, it will change what the loop is doing, because it will stop as soon as it hits an odd number. That may or may not be what you want, but is definitely different than the original code, which continues through the whole array.

If you do want to iterate over the whole array, it's probably best to use the foreach version of iteration, and let the compiler produce the fastest code:

for (int num : numArray) {
    if (num % 2 == 0) {
        evenSum += num;
        outputArea.append(num).append("\n");
    }
}
xs0
  • 2,990
  • 17
  • 25
0

I presume you are summing even numbers in the ArrayList, you asked for more efficiency so here you go, instead of using .get() we will use iterator as it will speed things up and we will use another faster method for checking if number is even:

package com.company;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Main {


    public static void main(String[] args) throws InterruptedException {

     int sum = 0;
     List<Integer> numbers = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));

     Iterator<Integer> iter = numbers.iterator();
     int num;

     while(iter.hasNext()){
         if (((num = iter.next()) & 1) == 0) // number is even
             sum += num;
     }

        System.out.println("Sum of all even numbers is: " + sum);

    }

}

If you had a really big list it would speed things up quite a bit compared to the original code.

EDIT:

Here are resources that you can study:

http://www.javainterviewpoint.com/check-number-even-odd-modulo-division-operators-java/

Iterator vs for

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35
  • that's a fair bit of speculation as we don't know what numArray actually is. Using an iterator boxing Integers might be actually slower. ArrayList's `Itr` class is only slightly faster as it skips a range check. For most use cases, readability is more important than micro-optimizations like this. – Jochen Bedersdorfer Dec 05 '17 at 22:39
  • Also using the bitwise operation might change the result (https://stackoverflow.com/questions/3072665/bitwise-and-in-place-of-modulus-operator). I also doubt it would be faster as the compiler would optimize it if possible – jontro Dec 05 '17 at 22:46
  • @jontro it won't cause him any erroneous results in the case as I specified above (non-negative integers), I would of actually provided him with additional resources about bitwise specifically on this topic, but I just can't remember the website I read/watched about it. – whatamidoingwithmylife Dec 05 '17 at 23:07