Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 遍历数组,求和并将结果放入新数组_Java_Arrays_Arraylist_Iteration - Fatal编程技术网

Java 遍历数组,求和并将结果放入新数组

Java 遍历数组,求和并将结果放入新数组,java,arrays,arraylist,iteration,Java,Arrays,Arraylist,Iteration,我有一个装满整数的arrayList,我需要遍历这个arrayList,将数字相加直到达到阈值,然后将结果和放入第二个arrayList槽中,然后返回到我在原始arrayList中停止的位置,继续迭代和求和,直到达到阈值,然后将其放入第二个插槽,依此类推,直到所有40个原始项都已求和并放入较小的arrayList 我曾想过使用两个嵌套循环,但无法使这两个循环一起工作 我对Java非常陌生,不知道如何做到这一点。任何建议都会有帮助 这就是我到目前为止所做的: int threshold = 12;

我有一个装满整数的arrayList,我需要遍历这个arrayList,将数字相加直到达到阈值,然后将结果和放入第二个arrayList槽中,然后返回到我在原始arrayList中停止的位置,继续迭代和求和,直到达到阈值,然后将其放入第二个插槽,依此类推,直到所有40个原始项都已求和并放入较小的arrayList

我曾想过使用两个嵌套循环,但无法使这两个循环一起工作

我对Java非常陌生,不知道如何做到这一点。任何建议都会有帮助

这就是我到目前为止所做的:

int threshold = 12;
int sumNum = 0;
int j = 0;
int arr1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
for (int i = 0; i < arr1.length; i++) {
  while (sumNum <= threshold) {
  sumNum += arr[j];
  j++
}//end while

}//end for

实际上,只需一个循环就可以做到这一点。你不需要搬回去,就呆在原地,继续做下去

public static ArrayList<Integer> sums(ArrayList<Integer> arr, int threshold){
    ArrayList<Integer> sumArr = new ArrayList<Integer>();
    int s = 0;  //Sum thus far, for the current sum
    for(int i : arr){
        s += i;             //Add this element to the current sum
        if(s >= threshold){ //If the current sum has reached/exceeded the threshold
            sumArr.add(s);  //Add it to the sumArray, reset the sum to 0.
            s = 0;
        }
    }
    return sumArr;
}
您可以将输入参数从ArrayList更改为int[]或Integer[],而无需任何麻烦。为每个循环万岁

使用上述代码:

public static void main(String[] args){
    ArrayList<Integer> i = new ArrayList<Integer>();
    //create arraylist 1..20
    for(int x = 1; x <= 20; x++){
        i.add(x);
    }

    System.out.println(sums(i).toString());
}

像这样的怎么样:

/**
 * Sequentially adds numbers found in the source array until the sum >= threshold.
 * <p/>
 * Stores each threshold sum in a separate array to be returned to the caller
 * <p/>
 * Note that if the last sequence of numbers to be summed does not meet the threshold,
 * no threshold sum will be added to the result array
 * 
 * @param numbersToSum The source number list
 * @param threshold The threshold value that determines when to move on to 
 * the next sequence of numbers to sum
 * 
 * @return An array of the calculated threshold sums
 */

public static Integer[] sumWithThreshold(int[] numbersToSum, int threshold)
{
    List<Integer> thresholdSums = new ArrayList<Integer>();

    if (numbersToSum != null)
    {
        int workingSum = 0;

        for (int number: numbersToSum)
        {
            workingSum = workingSum + number;

            if (workingSum >= threshold)
            {
                thresholdSums.add(workingSum);
                workingSum = 0;
            }
        }
    }

    return thresholdSums.toArray(new Integer[thresholdSums.size()]);
}

public static void main(String[] args)
{
    int[] testNumbers = 
    {
            1,2,3,4,5,6,7,8,9,10,
            11,12,13,14,15,16,17,18,19,20,
            21,22,23,24,25,26,27,28,29,30,
            31,32,33,34,35,36,37,38,39,40};

    int[] thresholds = {1, 42, 100, 200};

    for (int threshold: thresholds)
    {
        System.out.println("Threshold sums for threshold = " + threshold + ":\n" + Arrays.toString(sumWithThreshold(testNumbers, threshold)));
    }
}

我会同意这个答案。注意,这假设arr是一个实际的ArrayList,而不是问题中提供的原始数组。没错,尽管引用第一句话:我有一个充满整数的ArrayList…考虑到问题的性质,我会说可能会有一些混淆。似乎有可能。array vs ArrayList是一个单独的问题,在这里和其他地方都有很好的记录。同时,代码以任何方式工作,只需更改第一个参数的类型以满足您的需要。
15
13
17
21
12
13
14
/**
 * Sequentially adds numbers found in the source array until the sum >= threshold.
 * <p/>
 * Stores each threshold sum in a separate array to be returned to the caller
 * <p/>
 * Note that if the last sequence of numbers to be summed does not meet the threshold,
 * no threshold sum will be added to the result array
 * 
 * @param numbersToSum The source number list
 * @param threshold The threshold value that determines when to move on to 
 * the next sequence of numbers to sum
 * 
 * @return An array of the calculated threshold sums
 */

public static Integer[] sumWithThreshold(int[] numbersToSum, int threshold)
{
    List<Integer> thresholdSums = new ArrayList<Integer>();

    if (numbersToSum != null)
    {
        int workingSum = 0;

        for (int number: numbersToSum)
        {
            workingSum = workingSum + number;

            if (workingSum >= threshold)
            {
                thresholdSums.add(workingSum);
                workingSum = 0;
            }
        }
    }

    return thresholdSums.toArray(new Integer[thresholdSums.size()]);
}

public static void main(String[] args)
{
    int[] testNumbers = 
    {
            1,2,3,4,5,6,7,8,9,10,
            11,12,13,14,15,16,17,18,19,20,
            21,22,23,24,25,26,27,28,29,30,
            31,32,33,34,35,36,37,38,39,40};

    int[] thresholds = {1, 42, 100, 200};

    for (int threshold: thresholds)
    {
        System.out.println("Threshold sums for threshold = " + threshold + ":\n" + Arrays.toString(sumWithThreshold(testNumbers, threshold)));
    }
}
Threshold sums for threshold = 1:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
Threshold sums for threshold = 42:
[45, 46, 45, 54, 63, 47, 51, 55, 59, 63, 67, 71, 75, 79]
Threshold sums for threshold = 100:
[105, 105, 115, 110, 126, 105, 114]
Threshold sums for threshold = 200:
[210, 225, 231]