2014-09-23 131 views
-1

我有一個用整數填充的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 

回答

2

實際上,你可以只用一個循環做到這一點。你不需要退後,只需待在原地,繼續前進即可。

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()); 
} 
+1

我會爲這個答案得到答案。 – andrex 2014-09-23 04:02:35

+1

注意,這裏假定'arr'是一個實際的'ArrayList',而不是問題中提供的基本數組。 – voidHead 2014-09-23 04:05:16

+0

雖然引用第一句話:「我有一個arrayList,裏面填充整數...」 – Mshnik 2014-09-23 04:06:48

0
package com.test; 

import java.util.ArrayList; 
import java.util.List; 

public class Test { 
    public static void main(String[] args) { 
     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 }; 
     List myList = new ArrayList(); 

     for (int i = 0 ; i < arr1.length ; i++) { 
      sumNum += i; 
      if (sumNum >= threshold) { 
       myList.add(sumNum); 
       sumNum = 0; 
      } 
     } 

     for (int a = 0 ; a < myList.size() ; a++) { 
      System.out.println(myList.get(a)); 
     } 
    } 
} 

輸出

15 
13 
17 
21 
12 
13 
14 
0

怎麼是這樣的:

/** 
* 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))); 
    } 
} 

產生以下OU輸入:

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]