the list basically needs to go from (e.g.) 3 2 4 into a 3 5 2 6 4 , where 5 is a sum of 3 and 2 and 6 is a sum of 2 and 4. numbers in a list are typed manually and a list can have an infinite size.
i think i have the base(if you can call it that), but I'm not sure how exactly I can sum the specific numbers in a list. Here's what I got so far
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> list = new LinkedList<Integer>();
int number;
while (true){
number = sc.nextInt();
if(number==0)break;
if(list.size()==0)
list.add(number);
else{
// here's where I think the sum of numbers should be
}
list.add(i,number);
}
}
System.out.println("result:");
for (int n : list) {
System.out.print(n " ");
}
sc.close();
}
}
CodePudding user response:
I don't know what's that i
you are using (I don't see it declared anywhere), but you don't need it.
Your loop can behave as follows:
int number;
while (true) {
number = sc.nextInt();
if(number==0)
break;
if(list.size() == 0) {
list.add(number);
} else{
list.add(list.get(list.size()-1) number);
list.add(number);
}
}
EDITED: I see that you want the sum of each pair of consecutive input numbers.
- If the list is empty, you just add the first number to the list.
- Otherwise, you add the sum of the last element added to the list and the new input number, and then you add the new input number.
CodePudding user response:
A more versatile approach would be to read the user's input and process the List separately, by splitting these responsibilities as the first principle of SOLID suggests. That would allow to reuse the logic regardless of where the list is coming from.
Create a new List
One of the ways to address this problem would be to generate a new List and populate it with elements and their sums from the original list while iterating over the original list.
That's how it might look like:
public static List<Integer> addSumBetweenElements(List<Integer> list) {
if (list.isEmpty()) return Collections.emptyList();
List<Integer> result = new ArrayList<>();
int prev = list.get(0);
result.add(prev);
for (int i = 1; i < list.size(); i ) {
int next = list.get(i);
result.add(prev next);
result.add(next);
prev = next;
}
return result;
}
main()
public static void main(String[] args) {
System.out.println(addSumBetweenElements(List.of(1, 2, 3)));
}
Output:
[1, 3, 2, 5, 3]
ListIterator
- Modify the Original List
If your goal is to modify the original list, then you can make use of the functionality offered by the ListIterator
.
The key thing to remember about Iterators is that they always occupy position in between the elements (or before the very first, or right after the last element). And the method ListIterator.add()
is being invoked, a new element gets inserted right after the previous (which can be retrieved by previous()
call) and next element (which can be obtained by invoking next()
).
public static List<Integer> addSumBetweenElements(List<Integer> list) {
ListIterator<Integer> iterator = list.listIterator();
int prev = iterator.next();
while (iterator.hasNext()) {
int next = iterator.next(); // advance one position forward to retrieve next element
iterator.previous(); // move one step back (in between the previous and next elements)
iterator.add(prev next); // insert a new element
iterator.next(); // move one step forward (past the next element which has been processed)
prev = next;
}
return list;
}
main()
public static void main(String[] args) {
System.out.println(addSumBetweenElements(new LinkedList<>(List.of(1, 2, 3))));
}
Output:
[1, 3, 2, 5, 3]