2016-11-16 40 views
-1

我应该从“种群”数组中的所有种群中取得一定的最小值并将它们返回到数组中。我可以为我的新数组获取正确数量的元素,并正确地分配最后一个值,但剩下的值会返回为零!使用现有阵列的元素填充数组

class Canada { 
     private String[] provinces; 
     private int[] populations; 
     private int[] array; 
     private int i; 

     public static final int NO_SUCH_PROVINCE = -1; 
     public static final int O = 0; 
     public static final int Q = 1; 
     public static final int BC = 2; 
     public static final int A = 3; 
     public static final int M = 4; 
     public static final int S = 5; 
     public static final int NS = 6; 
     public static final int NB = 7; 
     public static final int NL = 8; 
     public static final int PE = 9; 
     public static final int NT = 10; 
     public static final int Y = 11; 
     public static final int N = 12; 

    public Canada() { 
     provinces = new String[13]; 
     provinces[O] = "Ontario"; 
     provinces[Q] = "Quebec"; 
     provinces[BC] = "British Columbia"; 
     provinces[A] = "Alberta"; 
     provinces[M] = "Manitoba"; 
     provinces[S] = "Saskatchewan"; 
     provinces[NS] = "Nova Scotia"; 
     provinces[NB]= "New Brunswick"; 
     provinces[NL] = "Newfoundland and Labrador"; 
     provinces[PE] = "Prince Edward Island"; 
     provinces[NT] = "Northwest Territories"; 
     provinces[Y] = "Yukon"; 
     provinces[N] = "Nunavut"; 

     populations = new int[13]; 
     populations[O] = 12851821; 
     populations[Q] = 7903001; 
     populations[BC] = 4400057; 
     populations[A] = 3645257; 
     populations[M] = 1208268; 
     populations[S] = 1033381; 
     populations[NS] = 921727; 
     populations[NB] = 751171; 
     populations[NL] = 514536; 
     populations[PE] = 142204; 
     populations[NT] = 141462; 
     populations[Y] = 33897; 
     populations[N] = 31906;    
    } 

    public int[] getPopulatiosnAboveMin(int min) {       
     int i = 0; 

     while(i < populations.length) { 
      if(populations[i] > min){ 
       array = new int[i + 1]; 
       array[i] = populations[i]; 
      } 
      i++; 
     }  
     return(array); 
    }  
    } 
+0

您应该使用地图和班级。 – SLaks

+1

每次通过while循环分配一个新数组,并且只填充它的最后一个成员看起来不正确... – John3136

回答

0

每当您循环访问populations.length时,都会重新创建该数组。这就是为什么它最终只有一个值。

使用for-each循环试试这个代码(这是一个有点乱,但它应该完成这项工作):

public int[] getPopulatiosnAboveMin(int min) { 

      int nextIndex = 0; 
      int[] firstArray = new int[populations.length]; 

      //Copy values larger then min to firstArray 
      for(int pop : populations) { 
       if(pop > min) { 
        firstArray[nextIndex++] = pop; 
       } 
      } 

      //Trim extra 0 values: 

      //Count number of non 0 values: 
      int non0Count = 0; 
      for (int i : firstArray) { 
       if (i != 0) { 
        non0Count++; 
       } 
      } 

      array = new int[non0Count]; //Initialize array with count of valid, non-0 values. 

      //Copy values from firstArray to array: 
      int index = 0; 
      for (int i : firstArray) { 
       if (i != 0) { 
        array[index++] = i; 
       } 
      } 

      return array; 
     } 

而且,SLaks在评论中提到,考虑创建一个类来表示你的省(和地区;)),或查看使用地图的信息,例如HashMap

+0

非常感谢你,这非常有帮助!我能否确切地问:“:”在循环中做了什么? – zhuangzi

+0

这种循环称为[_for-each_](http://stackoverflow.com/documentation/java/118/basic-control-structures/471/for-each#t=20161116054347577476)循环。它与常规_for_循环略有不同,可用于迭代_collections_和_arrays_。它如下所示:_for(** Type ** ** Var-Name ** **:** ** Array/Collection-to-Loop-through **)_。每个循环,**类型**的** Var-Name **将是您循环访问的数组或集合中的下一个值。很高兴我能提供帮助,不要忘记标记接受的答案,让相同问题的人更容易找到答案。 :) – Gulllie