Skip to content

Instantly share code, notes, and snippets.

@Yur-ok
Created December 18, 2015 12:00
Show Gist options
  • Save Yur-ok/70a69381db2fe1f3ee0a to your computer and use it in GitHub Desktop.
Save Yur-ok/70a69381db2fe1f3ee0a to your computer and use it in GitHub Desktop.
package Lesson3.KeyPoint2;
/**
* Created by Юрий on 18.12.2015.
*/
public class OddElementSum {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int[] arr1 = {1, 2};
int[] arr2 = {1};
int[] arr4 = null;
System.out.println(oddElementSum(arr));
System.out.println(oddElementSum(arr1));
System.out.println(oddElementSum(arr2));
System.out.println(oddElementSum(arr4));
}
static long oddElementSum(int data[]) {
if (data != null && data.length >= 2) {
int sum = 0;
for (int i = 1; i < data.length; i += 2) {
sum += data[i];
}
return sum;
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment