how to print a list of numbers with an interspersed sum?

Asked

Viewed 133 times

0

I have this list of 1 to 10.

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Code:

List <Long> lista = new ArrayList();

for (long a = 1; a <= 10; a++) {
 lista.add(a);
 System.out.print(lista);
}

I would need a sum interspersed in that list, with a variable.

For example:

List <Long> lista = new ArrayList();

    long x,z=200;

for (long a = 1; a <= 10; a++) {

  x=z+a;
  lista.add(x);

        }
 System.out.print(lista);
    }
}

Instead of printing the normal list + 200:

[201, 202, 203, 204, 205, 206, 207, 208, 209, 210]

I wanted the sum to be interspersed..

[1, 202, 3, 204, 5, 206, 7, 208, 9, 210]

And I also wish I could change that distance from how many jumps to give the sum on the list could be 2 in 2 or 5 in 5... of 30 in 30... you understand.. depending on the size of the list... list of 1 for 100 of 1 for 10000...

Running on the Repl: https://repl.it/repls/ShorttermUnfortunatePasswords

  • But do you want to add a list and an incremental number ? two lists ? two incremental numbers ? You speak of a list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] but then does not use it in sum. Moreover do not walk from 1 in 1 in that list crosses the boundaries, and unless it is "circular" it will not work. All these details are not clear in the question.

  • Good evening @Isac, I added the list to the number 200, yes I need that only an incremental number disappear with on this jump list can be 2 in 2... 5 in 5... 3 in 3... I could change that distance that it travels the sum..

  • Things remain unclear in order to have a satisfactory answer. How would it work to jump from 3 to 3 in this list with only 10 elements ? Make it clear

  • list from 1 to 10.... skipping elements from 3 to 3... and adding with the number 500... [1, 2, 503, 4, 5, 506, 7, 8, 509, 10]

  • And where does that come from 500 that would be a number that increases or these come from another list ?

  • this number increments, it can come from a variable only....

Show 1 more comment

2 answers

4


If you already have the list built, and want to apply a sum, jumping from x in x elements, just a for with an increment of x in x that stops when it reaches the number of items on the list:

int avanco = 3;
int numSoma = 500;
for (int i = avanco - 1; i < lista.size(); i += avanco) {
    lista.set(i, lista.get(i) + numSoma);
}

System.out.println(lista);

To a list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] the above code yields the following result:

[1, 2, 503, 4, 5, 506, 7, 8, 509, 10]

Check in with Ideone

Replare that I changed the value of each item in the list with the method set and I picked up the previous value with get. The beginning of for had to be with avanco - 1 because arrays start at 0.

However, a better solution would be to immediately generate the list with the elements you want, thus avoiding unnecessary processing:

int avanco = 3;
int numSoma = 500;
List <Long> lista = new ArrayList<>();
for (long a = 1; a <= 10; ++a){
    lista.add(a % avanco == 0 ? a + numSoma: a);
}

System.out.println(lista); //[1, 2, 503, 4, 5, 506, 7, 8, 509, 10]

See also this example in Ideone

Now just add the a in normal cases, and when the current number is multiple of the avanco adds the sum of the two. So the list is all generated at once with the sums you want.

  • Splendid! Exceptional! Exquisite! Sensational! Versatile and subtly optimized =D

  • my dear, how would the same program work for numbers in "Biginteger" because I can’t make this method work using Biginteger...

  • @Felipe BigInteger is different in several senses, because it forces the mathematical operations to be done with the methods of the object itself. Instead of adding with + would have to add up using the method add. However it is a little beyond the original question (perhaps it would be better to open a new question with this new doubt). Anyway, I always advise you to look at the documentation to be aware of the methods that exist for the BigInteger.

0

This answer answers your problem.

List <Long> lista = new ArrayList();
long x=200;
long pulo = 3;
for (long a = 1; a <= 10; a++) {
 if((a % pulo) == 0){
  lista.add(x+a);
 }else{
   lista.add(a);
 }
}
System.out.print(lista);
  • Genie, thank you so much bro!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.