How to create a method of finding sums of array elements in Java and invoke it to different arrays?

In the problem to the counting of service time, the queue of an arbitrary customer at N Cass turn is given an array of integers int[] customers, where every element of a customers[i] is the service time of the ith customer.

In the process of solving it was necessary to sum the array elements in the case where we have N=1:

.........
 } else if (n == 1) {
 for (int i = 0; i < customers.length; i++ ) {
 sum += customers[i];
}
}
.........
return sum;


However, the decision required to find the sum of elements from two arrays - nCustomers and restOfCustomers.
I create a method that allows to sum the elements of an arbitrary array:

public int sumOfArray(int ary[]) {
 int s = 0;
 for (int i = 0; i < ary.length; i++) {
 s += ary[i];
}
 return s;
 }


I now syntactically correct to call it to all three arrays in the code - customers nCustomers and restOfCustomers?
June 3rd 19 at 19:39
2 answers
June 3rd 19 at 19:41
If I understand correctly and you want the sum of elements in three arrays, then:
int sumOfAll = sumOfArray(arr1) + sumOfArray(arr2) + sumOfArray(arr3)

Or you can create a method with varargs, which will count the sum of an arbitrary number of arrays:
public int sumOfArray(int[]... ary) {
 /// your code
 }

Or swipe through the stream:
int sumOfAll = IntStream.of(arr1).sum() + IntStream.of(arr2).sum() + IntStream.of(arr3).sum();
No, to summarize the three arrays do not need.

1) "If the element is one, then why cycle?" - When running one office - N=1 - then the time required to service a queue set, for example, an array [2, 3, 1, 5, 7] - it is simply the sum of array elements: 2+3+1+5+7=18.

2) In the course of solving the problem when the number of people in the queue does not exceed the number of working desks, I just find the maximum element of the array customers:

} else if (customers.length <= n) {
 int customerMax = customers[0];
 for (int i = 0; i < n; i++) {
 if (customers[i] > customerMax) {
 customerMax = customers[i];
}
}
 return customerMax;
 }


3) In the case when the number of people in the queue is greater than the number of working banks (the customers.length > n && n > 1), I need to start the counter and to decrease the number by one all the elements of the array nCustomers (it's coming up to n banks n customers in the queue), while, as soon as some of its elements set to zero for the next iteration I in his place, put the first element of the array balance queue restOfCustomers[0], and the array restOfCustomers cut for this element. And all this occurs while the sum of elements in nCustomers will not be zero, and the array restOfCustomers is empty - zero length. - nikita.Stracke commented on June 3rd 19 at 19:44
I do not understand what the issue is. Explain normal. - jonathan.Prosacco commented on June 3rd 19 at 19:47
can't properly call a method to an array.

It would seem that you need to do this:

} else if (n == 1) {
 int sum = sumOfArray(customers);
 }


But I have an error:


error: non-static method sumOfArray(int[]) cannot be referenced from a static context
int sum = sumOfArray(customers);
- nikita.Stracke commented on June 3rd 19 at 19:50
June 3rd 19 at 19:43
I now do that
List<string> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("4");
list.add("1");

 int summ = list.stream()
 .map(s -> Integer.parseInt(s)) // converting from String to int
 .reduce( (s1,s2) -> s1 + s2 ) // amount
 .orElse(0); // default value
 System.out.println(summ);</string>

Instead of list - use your collection of objects.
And inside the map(....) take the desired value of the field.

If SIU with objects that can
public class Reduce {
 public static class Offer {
 int price;
 public Offer (int price) {
 this.price = price;
}

 public int getPrice() {
 return price;
}
}
 public static void main(String args[]) {
 List< Offer > list = new ArrayList<>();
 list.add(new Offer(1));
 list.add(new Offer(2));
 list.add(new Offer(4));
 list.add(new Offer(1));

 int summ = list.stream()
 .map(Offer::getPrice) // take the price
 .reduce( (s1,s2) -> s1 + s2 ) // amount
 .orElse(0); // default value

System.out.println(summ);
}
}

Find more questions by tags ArraysJava