2011-11-17 65 views
1

我有此代碼: indexOfChromosomes:48 indexOfGens:20 double截斷; double Crossover; double mutation; int Generation;錯誤:Android上的java.lang.ArrayIndexOutOfBoundsException

private Context context; 

int indexOfChromosomes; 
int indexOfGens; 
int gensNumber; 
int chromosomesNumber; 

public AdapterDB(int Bits, double Truncation, double Crossover, double Mutation, int Chromosomes, int Generation, Context ctx) 
{ 
    this.indexOfGens = Bits; 
    this.Truncation = Truncation; 
    this.Crossover = Crossover; 
    this.Mutation = Mutation; 
    this.indexOfChromosomes = Chromosomes; 
    this.Generation = Generation; 
    this.context = ctx; 
    DBHelper = new DatabaseHelper (context); 
} 

String [][] population = new String[indexOfChromosomes][indexOfGens]; 

    public void generateChromosome() 
{  
    for(chromosomesNumber = 0; chromosomesNumber < indexOfChromosomes ; chromosomesNumber++) 
    { 
        int o = 0; 
     for(gensNumber = 0; gensNumber < 12 ; gensNumber++) 
     { 
      Cursor l = db.rawQuery("SELECT _id, key_foodstuff, key_calorie, key_carbohydrate, key_fat, key_protein FROM (food INNER JOIN categories ON food.key_nocategory = categories.nocategories) WHERE key_type='primary' AND _id!=164 AND (key_carbohydrate!=0 OR key_protein!=0 OR key_fat!=0) ORDER BY RANDOM() LIMIT 1", null); 

      if((l.moveToFirst()) && (l!=null)) 
      { 
       if (o == indexOfGens) 
       { 
        gensNumber = 0; 
        sumOfCarbohydrateMor = 0; 
        sumOfFatMor = 0; 
        sumOfProteinMor = 0; 
        sumOfCalorieMor = 0; 
        o = 0; 
       } 

       population[chromosomesNumber][gensNumber] = l.getString(0); 
       morning_food[k] = l.getString(3); 
       sumOfCarbohydrateMor = sumOfCarbohydrateMor + Double.parseDouble(morning_food[k]); 
       morning_food[f] = l.getString(4); 
       sumOfFatMor = sumOfFatMor + Double.parseDouble(morning_food[f]); 
       morning_food[p] = l.getString(5); 
       sumOfProteinMor = sumOfProteinMor + Double.parseDouble(morning_food[p]); 
       morning_food[c] = l.getString(2); 
       sumOfCalorieMor = sumOfCalorieMor + Double.parseDouble(morning_food[c]); 
       if (((sumOfCarbohydrateMor >= (morning_car-(morning_car*0.2))) && (sumOfCarbohydrateMor <= morning_cal*1.1)) && ((sumOfProteinMor >= (morning_pro-(morning_pro*0.2))) && (sumOfProteinMor <= morning_pro*1.1)) && ((sumOfFatMor >= (morning_fat-(morning_fat*0.2))) && (sumOfFatMor <= morning_fat*1.1))) 
       //if((sumOfCarbohydrateMor > (morning_car*0.6)) && (sumOfProteinMor > (morning_pro*0.7)) && (sumOfFatMor > (morning_fat*0.8))) 
       { 
        Log.e("lala", "lalala"); 
        break; 
       } 

       if ((sumOfCarbohydrateMor > (morning_car*1.1)) || (sumOfProteinMor > (morning_pro*1.1)) || (sumOfFatMor > (morning_fat*1.1)) || (sumOfCalorieMor > (morning_cal*1.1)) 
       { 

        morning_food[k] = l.getString(3); 
        sumOfCarbohydrateMor = sumOfCarbohydrateMor - Double.parseDouble(morning_food[k]); 
        morning_food[f] = l.getString(4); 
        sumOfFatMor = sumOfFatMor - Double.parseDouble(morning_food[f]); 
        morning_food[p] = l.getString(5); 
        sumOfProteinMor = sumOfProteinMor - Double.parseDouble(morning_food[p]); 
        morning_food[c] = l.getString(2); 
        sumOfCalorieMor = sumOfCalorieMor - Double.parseDouble(morning_food[c]); 
        gensNumber--; 
        o++; 

       } 

      } 
     } 

,並錯誤行:48

錯誤說:java.lang.ArrayIndexOutOfBoundsException

什麼想法? THXü

+0

你確定它們實際上是48和20嗎?你有沒有做過任何調試? – SuperTron

+0

是的,我確定,我已經在LogCat – Michelle

+0

上顯示不足信息。你給構造函數什麼值? –

回答

1

這是你的問題:

String [][] population = new String[indexOfChromosomes][indexOfGens]; 

出現這種情況你的構造,因此您的構造函數之前執行,當indexOfChromosomesindexOfGens都是仍爲0把初始化您的構造函數。下面是顯示了同樣的問題一個簡單的例子:

public class Test { 
    private int size; 

    public Test(int size) { 
     this.size = size; 
    } 

    private String[] array = new String[size]; 

    public static void main(String[] args) { 
     Test t = new Test(5); 
     System.out.println(t.array.length); 
    } 
} 

和固定版本:

public class Test { 
    private int size; 
    private String[] array; 

    public Test(int size) { 
     this.size = size; 
     array = new String[size]; 
    } 


    public static void main(String[] args) { 
     Test t = new Test(5); 
     System.out.println(t.array.length); 
    } 
} 

注意,相對於構造函數的變量聲明的定位使得來執行流程沒有什麼區別。

編輯:至於爲什麼它現在循環永遠 - 在你的代碼中你有:

if (o == indexOfGens) 
{ 
    gensNumber = 0; 
    ... 
} 

將內環重置回(幾乎)開始(並不完全是開始,因爲gensNumber將在循環體的末尾增加,在下一次迭代開始之前)。

這並不完全清楚你想要做什麼,但我懷疑這沒有幫助。

我還鼓勵您使用當地變量儘可能 - 這是非常不尋常的使用實例變量作爲循環計數器,例如。

最後,我鼓勵你將你的大方法分解成更小的方法以提高可讀性。

+0

Yes Thx a it works ,但它會永遠循環,怎麼樣? – Michelle

+0

@Michelle:這表明你的代碼中有多個錯誤:)我建議你仔細看看 - 我只是試圖解決你提出的第一個問題... –

+0

Hoo Thx你,我已經試過了我很困惑我應該怎麼做,上次我試圖把String [] [] population = new String [indexOfChromosomes] [indexOfGens];在generateChromosomes()上,它也可以工作,但是也會永遠循環。無論如何Thx很多= D – Michelle

1

嘗試設置

population = new String[indexOfChromosomes][indexOfGens]; 

您adapterDB內。

看起來當你初始化人口普查看人口,indexOfChromosomes和indexOfGens尚未初始化,所以要創建大小爲0的數組像這樣,當你調用

population[chromosomesNumber][gensNumber] 

你的java.lang。ArrayIndexOutOfBoundsException

+0

是的,它有很多工作,但它會永遠循環,如何? – Michelle

+0

你有一個gensNumber--;在你的if裏面。如果(((sumOfCarbohydrateMor> =(morning_car-(morning_car * 0.2))))&&(sumOfCarbohydrateMor <= morning_cal * 1.1))也許這是造成問題,並且不讓你離開for循環 – marimaf

+0

。 &&((sumOfProteinMor> =(morning_pro-(morning_pro * 0.2)))&&(sumOfProteinMor <= morning_pro * 1.1))&&((sumOfFatMor> =(morning_fat-(morning_fat * 0.2)))&&(sumOfFatMor <= morning_fat * 1.1 ))) { Log.e(「lala」,「lalala」); 休息; } 爲什麼不讀取它?_ < – Michelle

1

這是因爲您在函數外定義了總體字符串,而沒有將indexOfChromosomes初始化爲48,將indexOfGens初始化爲20.嘗試在頂部定義總體並將其設置爲適配器中的新內容,在設置好後你的變量。就像這樣:

double Truncation; 
double Crossover; 
double Mutation; 
int Generation; 

private Context context; 

int indexOfChromosomes; 
int indexOfGens; 
int gensNumber; 
int chromosomesNumber; 

String [][] population; 

public AdapterDB(int Bits, double Truncation, double Crossover, double Mutation, int Chromosomes, int Generation, Context ctx) 
{ 
    this.indexOfGens = Bits; 
    this.Truncation = Truncation; 
    this.Crossover = Crossover; 
    this.Mutation = Mutation; 
    this.indexOfChromosomes = Chromosomes; 
    this.Generation = Generation; 
    this.context = ctx; 
    DBHelper = new DatabaseHelper (context); 

    //Create population after initializing variables. 
    population = new String[indexOfChromosomes][indexOfGens]; 
} 
+0

是的,Thx有很多功能,但它會永久循環使用,怎麼樣? – Michelle

相關問題