如何求和和数组的间隔?

时间:2018-04-19 20:24:13

标签: java arrays sum

ggplot(data=dataset, aes(x= dataset$count)) + 
        geom_histogram(binwidth = 3,
                       col="blue", 
                       fill="darkblue") + 
        labs(title="Retweets Distribution") +
        labs(x="Retweet number") +
        theme(plot.title = element_text(hjust = 0.5)) +
        scale_x_continuous(limits = c(0,250)) +
        scale_y_continuous(limits = c(0,250)) + expand_limits(x = 0, y = 0)
  

在这种情况下“sum”= 15,但如果我想将索引[0]到[3]的数字相加怎么办?

3 个答案:

答案 0 :(得分:3)

使用Arrays.stream()

public class AppendingObjectOutputStream extends ObjectOutputStream {

  public AppendingObjectOutputStream(OutputStream out) throws IOException {
    super(out);
  }

  @Override
  protected void writeStreamHeader() throws IOException {
    // do not write a header, but reset:
    // this line added after another question
    // showed a problem with the original
    reset();
  }

}

答案 1 :(得分:0)

为流添加限制:

limit

答案 2 :(得分:0)

在这种情况下,使用Arrays.stream():并转换您需要的时间间隔

示例:

import java.util.Arrays;
import java.util.Scanner;



    public class SumInterval{


        public static void main(String[] args) {
            // TODO Auto-generated method stub
                                    int[] a = {1 ,2 ,3 ,4 ,5};
                                     // entre index 
                                     int sum = Arrays.stream(a, 0, 2).sum();
                                    System.out.println("The sum is " + sum);
        }


    }

输出:

`The sum is` 3