Home » Java » java – How do I add an integer to every element of an array-Exceptionshub

java – How do I add an integer to every element of an array-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m doing a method called adversary that takes two parameters: an array of ints and a number int and it adds that number to each element of the array

How to&Answers:

Try this

public static void add(int[] a, int n ) { 
    for (int i=0; i<a.length;i++){
         a[i]=a[i]+n;
}}

Let me know if it helps