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

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

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

问题

我有这个方法头部:

public boolean legal(int total)

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

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

谢谢。

英文:

I have the method head

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)

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

Using Java 8 streams:

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

答案2

得分: 0

你可能需要像这样实现:

int[] arr = new int[] { 1, 2, 3, 4, 5 };

public boolean legal(int total) {
    int i = 0;
    int sum = 0;
    while (i < arr.length) {
        sum += arr[i];
        i = i + 1;
    }
    if (sum <= total) {
        return true;
    } else {
        return false;
    }
}
英文:

You may have to implement like this

int[] arr = new int[] { 1, 2, 3, 4, 5 };

public boolean legal(int total){
    int i=0;
    int sum=0;
    while (i &lt; arr.length) {
       sum += arr[i];
       i=i+1;
    } 
    if(sum &lt;= total){
       return true;
    }
    else{
      return false;
    }
}

答案3

得分: -1

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

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

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

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

英文:

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

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

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:

确定