2015-06-09 23 views
-1

嗨,亲爱的,我在这里发布我的整个班级,我遇到问题。首先我解释我的问题。我正在使用spinners生成一个arrayList“Items”,并且spinners和arrays中的元素完全相同然后在比较数组长度和ArayList大小之后,我想跳到下一个活动,但是现在比较数组时会出现问题,请帮我解决这个问题 在这里感谢我的活动引起:java.lang.ArrayIndexOutOfBoundsException:length = 8; index = 8

package com.example.mine4.pantryrecipes; 
 

 
import android.app.Activity; 
 
import android.content.Intent; 
 
import android.os.Bundle; 
 
import java.util.List; 
 
import java.util.ArrayList; 
 

 
import android.view.View; 
 
import android.widget.Toast; 
 
import android.widget.Button; 
 
import android.widget.TextView; 
 

 

 

 
public class AddItem extends Activity { 
 
    MultiSelectionSpinner spinner1,spinner2,spinner3,spinner4; 
 
    Button button1; 
 
    TextView tv; 
 
    private int count = 0; 
 
    List<String> Items=new ArrayList<>(); 
 
    List<String> veg=new ArrayList<>(); 
 
    List<String> spice=new ArrayList<>(); 
 
    List<String> dairy=new ArrayList<>(); 
 
    ArrayList<Boolean> subset = new ArrayList<>(); 
 
    String[] arrayRecipe = { "chicken","vegetable oil","ginger", 
 
      "onion", "garlic","potatoes","tomatoes","roasted peanuts", "peanut butter"}; 
 
    String[] arrayRecipe2 = { "chicken","garlic","vegetable oil", "tomatoes", 
 
      "Dijon mustard","breadcrumbs","Parmesan cheese","unsalted butter"}; 
 
    String[] arrayRecipe3 = { "chicken", "vegetable oil","unsalted butter", 
 
      "sugar", "garlic", "sauce"}; 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) 
 
    { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_add_item); 
 
     button1=(Button)findViewById(R.id.button1); 
 
     tv=(TextView)findViewById(R.id.tv); 
 
     String[] arrayItems = {"chicken"}; 
 
     String[] arrayVeg = { "vegetable oil", "ginger", 
 
       "garlic","potatoes" ,"tomatoes","onion"}; 
 
     String[] arraySpice = { "roasted peanuts", "sauce","Dijon mustard" }; 
 
     String[] arrayDairy = { "breadcrumbs", "Parmesan cheese", "unsalted butter","sugar", "peanut butter" }; 
 
     spinner1 = (MultiSelectionSpinner) findViewById(R.id.mySpinner1); 
 
     spinner2 = (MultiSelectionSpinner) findViewById(R.id.mySpinner2); 
 
     spinner3 = (MultiSelectionSpinner) findViewById(R.id.mySpinner3); 
 
     spinner4= (MultiSelectionSpinner) findViewById(R.id.mySpinner4); 
 

 
     spinner1.setItems(arrayItems); 
 
     spinner2.setItems(arrayVeg); 
 
     spinner3.setItems(arraySpice); 
 
     spinner4.setItems(arrayDairy); 
 

 
    } 
 
    public void onClick(View v) 
 
    { 
 
     Items=spinner1.getSelectedStrings(); 
 
     veg=spinner2.getSelectedStrings(); 
 
     spice=spinner3.getSelectedStrings(); 
 
     dairy=spinner4.getSelectedStrings(); 
 
     Items.addAll(veg); 
 
     Items.addAll(spice); 
 
     Items.addAll(dairy); 
 
//  for(int i=0;i<Items.size();i++) 
 
//   tv.append(Items.get(i)); 
 

 
     compareArray(); 
 
     //count++; 
 
    } 
 
    public void compareArray() 
 
    { 
 
     for(int j = 0 ; j < arrayRecipe.length ; j++) 
 
      { 
 
       for(int i = 0 ; i <Items.size() ; i++) 
 
       { 
 
        if((arrayRecipe[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
        if ((arrayRecipe2[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
        if ((arrayRecipe3[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
       } 
 
      } 
 

 
     if(subset != null) { 
 
      if ((arrayRecipe.length == subset.size())) { 
 
       Intent nextClass = new Intent(); 
 
       nextClass.setClass(AddItem.this, TenIngreRec.class); 
 
       startActivity(nextClass); 
 
       finish(); 
 
       Toast.makeText(getApplicationContext(), "arrayRecipe", Toast.LENGTH_LONG).show(); 
 
      } 
 
      if ((arrayRecipe2.length == subset.size())) { 
 
       Intent nextClass = new Intent(); 
 
       nextClass.setClass(AddItem.this, EightIngreRec.class); 
 
       startActivity(nextClass); 
 
       finish(); 
 
       Toast.makeText(getApplicationContext(), "arrayRecipe2", Toast.LENGTH_LONG).show(); 
 
      } 
 
      if ((arrayRecipe3.length == subset.size())) { 
 
       Intent nextClass = new Intent(); 
 
       nextClass.setClass(AddItem.this, EightIngreRec.class); 
 
       startActivity(nextClass); 
 
       finish(); 
 
       Toast.makeText(getApplicationContext(), "arrayRecipe3", Toast.LENGTH_LONG).show(); 
 
      } 
 
      else { 
 
       Toast.makeText(getApplicationContext(), "Un-Matched", Toast.LENGTH_LONG).show(); 
 
      } 
 
     } 
 
    } 
 
    @Override 
 
    public void onBackPressed() 
 
    { 
 
     // code here to show dialog 
 
     super.onBackPressed(); // optional depending on your needs 
 
     Intent intn = new Intent(AddItem.this,Exmain.class); 
 
     startActivity(intn); 
 
    } 
 
    
 
}

,并在此代码是阵列发生的主要问题indexOutOfBound

for(int j = 0 ; j < arrayRecipe.length ; j++) 
 
      { 
 
       for(int i = 0 ; i <Items.size() ; i++) 
 
       { 
 
        if((arrayRecipe[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
        if ((arrayRecipe2[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
        if ((arrayRecipe3[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
       } 
 
      }

再次感谢帮助我

+1

我认为你倒i和j指数...使用j arrayRecipe和我的项目 –

回答

0

array.Length()从1数始终在您的for循环

0

使用此

没有形成零使用 arrayRecipe.length -1

for(int j = 0 ; j < (arrayRecipe.length)-1 ; j++) 
 
      { 
 
       for(int i = 0 ; i <Items.size() ; i++) 
 
       { 
 
        if((arrayRecipe[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
        if ((arrayRecipe2[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
        if ((arrayRecipe3[i].equals(Items.get(j)))) 
 
        { 
 
         subset.add(true); 
 
         break; 
 
        } 
 
       } 
 
      }

0

两个问题:

  1. 您使用的arrayRecipe大小约束的arrayRecipearrayRecipe2arrayRecipe3所有三个阵列的大小不同的指标,从而导致你的错误。您需要3个不同的for循环,每个循环arrayRecipe

  2. 你的ij是在错误的地方。 Items.get(j)应该是Items.get(i),因为您使用的是Items.size来限制i的大小。同样,arrayRecipe.get(i)应该是arrayRecipe.get(j)

而且,这是事实,getLength()getSize()从1开始,而不是0开始计数,因此最终槽的指数是(getLenght() - 1),但在使用<的条款你的循环来处理这一点。

+0

欧文瑞安兄弟我用你的建议,但错误是相同的arrayRecipe3的条件。你可以重新检查请 –

+0

像我说的,错误是由于你使用arrayRecipe的大小为所有3个arrayRecipes,因为每个人都有不同的大小,所以如果arrayRecipe比arrayRecipe3大,你会得到一个outofbounds错误。你需要3个独立的“for”循环,1个用于arrayRecipe,arrayRecipe2和arrayRecipe3。 –

相关问题