d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Question About Java
Add Reply New Topic New Poll
Member
Posts: 856
Joined: Oct 10 2018
Gold: 0.00
Jan 30 2019 05:02am
Hey,

task: Calculate the sum over a list of integers. A loop should be used.

IS the following correct?

for (i = 1, i <= size(aListOfIntegers), i++) {
a = aListOfIntegers.get(i);
sum = sum + a)
}


thx
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Jan 30 2019 06:41am
Code
for (int number : aListOfIntegers) {
sum = sum + number;
}


You can just use the above definition instead, which loops over each number in the list and presents that directly to you.

Your example also has missing type information and a wrong method call:



Code
for (int i = 0; i < aListOfIntegers.size(); i++) {
int a = aListOfIntegers.get(i);
sum = sum + a;
}



Here's basically all the code you need:
Code
import java.util.ArrayList;
import java.util.List;

public class MyClass {
public static void main(String args[]) {
int sum = 0;
int sum2 = 0;
List<Integer> aListOfIntegers = new ArrayList<>();
aListOfIntegers.add(1);
aListOfIntegers.add(2);
aListOfIntegers.add(3);
aListOfIntegers.add(4);
aListOfIntegers.add(5);
aListOfIntegers.add(6);
aListOfIntegers.add(7);
aListOfIntegers.add(8);
aListOfIntegers.add(9);
aListOfIntegers.add(10);

for (int i = 0; i < aListOfIntegers.size(); i++) {
int a = aListOfIntegers.get(i);
sum = sum + a;
}

for (int number : aListOfIntegers){
sum2 += number;
}

System.out.println("Sum = " + sum);
System.out.println("Sum2 = " + sum2);
}
}


This post was edited by Klexmoo on Jan 30 2019 06:56am
Member
Posts: 856
Joined: Oct 10 2018
Gold: 0.00
Jan 30 2019 07:17am
Quote (Klexmoo @ Jan 30 2019 02:41pm)
Code
for (int number : aListOfIntegers) {
sum = sum + number;
}


You can just use the above definition instead, which loops over each number in the list and presents that directly to you.

Your example also has missing type information and a wrong method call:



Code
for (int i = 0; i < aListOfIntegers.size(); i++) {
int a = aListOfIntegers.get(i);
sum = sum + a;
}



Here's basically all the code you need:
Code
import java.util.ArrayList;
import java.util.List;

public class MyClass {
public static void main(String args[]) {
int sum = 0;
int sum2 = 0;
List<Integer> aListOfIntegers = new ArrayList<>();
aListOfIntegers.add(1);
aListOfIntegers.add(2);
aListOfIntegers.add(3);
aListOfIntegers.add(4);
aListOfIntegers.add(5);
aListOfIntegers.add(6);
aListOfIntegers.add(7);
aListOfIntegers.add(8);
aListOfIntegers.add(9);
aListOfIntegers.add(10);

for (int i = 0; i < aListOfIntegers.size(); i++) {
int a = aListOfIntegers.get(i);
sum = sum + a;
}

for (int number : aListOfIntegers){
sum2 += number;
}

System.out.println("Sum = " + sum);
System.out.println("Sum2 = " + sum2);
}
}


Thank you very much :)

Edit: Would it be possible to solve the task with a recursion ?

This post was edited by 05bb94 on Jan 30 2019 07:28am
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Jan 30 2019 08:53am
Quote (05bb94 @ 30 Jan 2019 14:17)
Thank you very much :)

Edit: Would it be possible to solve the task with a recursion ?


Yes, solving it with recursion would require a new function to be defined:

Code
public static int sumRecursive(List<Integer> numbers) {
if (numbers.isEmpty() == true ) {
return 0;
}
else {
return numbers.get(0) + sumRecursive(numbers.subList(1, numbers.size()));
}
}


The function takes one argument (the list of numbers to compute sum for) and adds the first number from the input list to the number obtained from a call to itself, on the remaining items from the input list.
A check is done on the list of numbers to end the recursion, since every recursion step decrements the size of the list by one.

This post was edited by Klexmoo on Jan 30 2019 08:55am
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Jan 30 2019 10:06pm
Code
aListOfIntegers.stream().mapToInt(Integer::intValue).sum()
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll