如何返回布尔语句,如果ArrayList的总和小于或等于最大数?

huangapple go评论122阅读模式
英文:

How to return boolean statement if the sum of an arraylist is less than or equal to a maximum number?

问题

我有这个方法头部:

  1. public boolean legal(int total)

该方法必须在ArrayList<Integer> numbers中的数值总和不超过参数total时返回true

请帮我思考如何实现这个方法。哪种类型的循环更适合?

谢谢。

英文:

I have the method head

  1. public boolean legal(int total)

that must return true if the sum of the ArrayList&lt;Integer&gt; numbers is not higher than the parameter total.

Please help me on how I could implement this. Which type of loop is preferred?

Thanks.

答案1

得分: 0

Sure, here is the translated content:

使用 Java 8 流(streams)

  1. public boolean legal(int total) {
  2. return numbers.stream()
  3. .mapToInt(Integer::intValue) // 将流转换为 IntStream 对象
  4. .sum() <= total;
  5. }
英文:

Using Java 8 streams:

  1. public boolean legal(int total) {
  2. return numbers.stream()
  3. .mapToInt(Integer::intValue) // converts the stream to an IntStream object
  4. .sum() &lt;= total;
  5. }

答案2

得分: 0

你可能需要像这样实现:

  1. int[] arr = new int[] { 1, 2, 3, 4, 5 };
  2. public boolean legal(int total) {
  3. int i = 0;
  4. int sum = 0;
  5. while (i < arr.length) {
  6. sum += arr[i];
  7. i = i + 1;
  8. }
  9. if (sum <= total) {
  10. return true;
  11. } else {
  12. return false;
  13. }
  14. }
英文:

You may have to implement like this

  1. int[] arr = new int[] { 1, 2, 3, 4, 5 };
  2. public boolean legal(int total){
  3. int i=0;
  4. int sum=0;
  5. while (i &lt; arr.length) {
  6. sum += arr[i];
  7. i=i+1;
  8. }
  9. if(sum &lt;= total){
  10. return true;
  11. }
  12. else{
  13. return false;
  14. }
  15. }

答案3

得分: -1

可能最简单的遍历列表的方法是使用 foreach 循环:

  1. public boolean legal(int total) {
  2. int sum = 0;
  3. for(int num : numbers) { // 遍历ArrayList numbers中的每个数字
  4. sum += num;
  5. }
  6. return sum <= total; // 如果总和小于等于total,则返回true
  7. }

与普通的for循环相比,使用foreach循环更加简便,因为您无需处理索引。然而,只有在集合具有索引的情况下才能使用foreach循环,对于您的情况,ArrayList具有索引。因此,使用foreach循环是遍历列表的最简单方法。

如果您的集合没有索引(例如,如果您使用了 Set),则可以使用普通的 for循环 或者 Iterator

英文:

Probably the simplest way to loop thotugh your list is by using a foreach loop

  1. public boolean legal(int total) {
  2. int sum = 0;
  3. for(int num : numbers) { //loop through every number in ArrayList numbers
  4. sum += num;
  5. }
  6. return sum &lt;= total; //return true if the sum is less than or equal to total
  7. }

A foreach loop is easier to use than a for loop since you do not to handle indeces. However a foreach loop can only be used if your collection have indices, which for your case an ArrayList have. Therefore a foreach loop is the easiest way to loop thorugh your list.

If your collection do not have indices (if your use for example a Set) you can instead use a regular for loop or an Iterator.

huangapple
  • 本文由 发表于 2020年4月8日 20:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/61100614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定