You are viewing a single comment's thread. Return to all comments →
Language: JAVA 15
We can solve this problem in two ways:
1) By using For Each Loop to iterate all elements in List and adding it to the variable sum :
public static int simpleArraySum(List<Integer> ar) { int sum= 0; for( int i : ar ){ sum = sum + i ; // i contains elements of List ar } return sum; }
2) And by using For Loop where we will add each element present in List using ListName.get(index) method to variable sum :
public static int simpleArraySum(List<Integer> ar) { int sum= 0; for( int i=0; i < ar.size(); i++ ){ sum = sum + ar.get(i) ; } return sum; }
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Array Sum
You are viewing a single comment's thread. Return to all comments →
Language: JAVA 15
We can solve this problem in two ways:
1) By using For Each Loop to iterate all elements in List and adding it to the variable sum :
2) And by using For Loop where we will add each element present in List using ListName.get(index) method to variable sum :