2
 // Find smallest year
 int pastMin=0;
 int currentMin=ontarioBankInfoInt.get(0);
 int currentMinIndex=0;
 int reps=(ontarioBankInfoInt.size()-1)/3;

 for(int x=0; x<=reps; x++){
   for (int i=0; i<ontarioBankInfoInt.size()-1; i++){
        if (ontarioBankInfoInt.get(i)<currentMin){
            if (ontarioBankInfoInt.get(i)>pastMin){
                currentMin = ontarioBankInfoInt.get(i);
                currentMinIndex = i;     
                pastMin = currentMin;
            }//If End
        }//If End       
   }//For End

   //Add other information  
   yearArray.add(currentMin);
 }//For End

NOTE: reps is the number of years it needs to find. The pupose of this code is so search an arrayList for the smallest year then add it to the "yearArray" then it finds the next smallest add adds it to the say array, rinse and repeat until it works through the entire array.

My problem is that it always returns the same year and im not sure why any help?

Thanks

4

3 回答 3

2

You aren't reinitializing currentMin and currentMinIndex so these will reach the minimum value, then they won't ever be reassigned.

You need to reinitialize them inside the loop.

for(int x=0; x<=reps; x++){

   int currentMin = Integer.MAX_VALUE;  // Do this here.
   //int currentMinIndex = -1;  // This is never used??

   for (int i=0; i < ontarioBankInfoInt.size() - 1; i++) {
        int value = ontarioBankInfoInt.get(i);
        if (value < currentMin && value > pastMin) {
            currentMin = value;
            //currentMinIndex = i;     
        }
   }

   pastMin = currentMin; // I also think you want this here.
   yearArray.add(currentMin);
}

Or you could just sort the list and take the first reps elements.

于 2012-06-15T13:15:09.170 回答
0

Look at this part:

if (ontarioBankInfoInt.get(i)<currentMin){
    if (ontarioBankInfoInt.get(i)>pastMin){
        currentMin = ontarioBankInfoInt.get(i);
        currentMinIndex = i;     
        pastMin = currentMin;
    }
}

The first time it gets into the inner part, it will set pastMin and currentMin to the same value. After that, how could any value be both less than currentMin and more than pastMin?

It's not really clear what you're trying to achieve, but that's why you'll only get into the middle part once.

于 2012-06-15T13:14:42.693 回答
0

This is a quick and brutal way to get a list of lowest ints, one for each year.

Collections.sort(ontarioBankInfoInt);
List<Integer> yearArray = new ArrayList<Integer>(ontarioBankInfoInt.subList(0, reps));

You'd probably want to copy the contents of ontarioBankInfoInt into a new array before sorting (if you want to preserve your original order).

This of course assumes that there are no repeat values in ontarioBankInfoInt.
{ 1,3,2,1,2 } for 3 years would give you { 1,1,2 } instead of { 1,2,3 }

If you don't want duplicate values, and you don't want to use a loop (and performance is not an issue), then you could:

  1. copy ontarioBankInfoInt into a Set (thus removing duplicates).
  2. back into a List.
  3. sort the list.
  4. get a sublist for the number of years you require.

But seriously, use a loop as described in other answers... :)

于 2012-06-15T14:10:36.167 回答