How to print a list with the result of all numbers in a sequence added to itself? (sequence) [closed]

-7

I have this sequence of numbers: 1,2,3. (It can be from 1 to infinity.)

I want to add each sequence number to it, for example:

The sequence has 3 numbers.

You need to add it like this:

(1)  
1+1=2  
1+2=3  
1+3=4  

(2)  
2+1=3  
2+2=4  
2+3=5 

(3)  
3+1=4  
3+2=5  
3+3=6 

The program should print the list with the results of each sequence number separately:

1 = (2,3,4)
2 = (3,4,5)
3 = (4.5.6)

    
asked by anonymous 13.06.2017 / 20:00

1 answer

1

I believe this example does what you need. The class receives the list of elements in the constructor. The sum method iterates twice in a nested way from the list and prints the result.

import java.util.ArrayList;
import java.util.List;

public class SomaLista {

    private List<Integer> lista = null;

    public SomaLista(List<Integer> lista) {
        this.lista = lista;
    }

    public void soma() {
        for (Integer externo : lista) {
            System.out.print(externo + "=");
            List<String> resultado = new ArrayList<String>();
            for (Integer interno : lista) {
                resultado.add(String.valueOf(interno + externo));
            }
            System.out.println("("+String.join(",", resultado)+")");
        }
    }

    public static void main(String args[]) {
        List<Integer> lista = new ArrayList<Integer>();

      for (int i=1; i<=1000000; i++){
  lista.add(i);
        }
        SomaLista sl = new SomaLista(lista);
        sl.soma();
    }

}
    
13.06.2017 / 20:34