Last active
March 11, 2016 11:34
-
-
Save alexejVasko/41e11bd7abec5014394a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ppackage forEach; | |
public class ForEach { | |
public static void main(String[] args) { | |
int[] data1 = {1,2,3,4}; | |
System.out.println(oddElementSum(data1)); | |
int[] data2 = null; | |
System.out.println(oddElementSum(data2)); | |
System.out.println(oddElementSum(new int[]{0})); | |
System.out.println(oddElementSum(new int[]{-1,-1,-1,-1})); | |
} | |
private static long oddElementSum(int[] data){ | |
long sum = 0; | |
if(data != null){ | |
for(int i = 0; i < data.length; i++){ | |
if(i%2 != 0){ | |
sum += data[i]; | |
}else continue; | |
} | |
}else sum = -1; | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment