引起:java.lang.ArrayIndexOutOfBoundsException:length = 8;索引= 8

时间:2015-06-09 07:22:14

标签: android arrays

嗨亲爱的,我在全班上课,我遇到了问题。首先我解释一下我的问题。我使用微调器生成一个arrayList“Items”,旋转器和数组中的元素完全相同然后在比较数组长度和ArayList大小之后我想跳到下一个活动但是现在比较数组时出现问题请帮我解决这个问题 谢谢,这是我的活动

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;
                    }
                }
            }

再次感谢帮助我

3 个答案:

答案 0 :(得分:0)

array.Length()始终从1开始计数而不是零使用 你的for循环

中的 arrayRecipe.length -1

答案 1 :(得分:0)

使用此

&#13;
&#13;
 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;
                    }
                }
            }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

两个问题:

  1. 您使用arrayRecipe的大小来绑定arrayRecipe2arrayRecipe3for的索引所有三个数组的大小不同,因此导致你的错误。您需要3个不同的arrayRecipe循环,每个i一个。

  2. 您的jItems.get(j)位置错误。 Items.get(i)应该是Items.size,因为您使用i来限制arrayRecipe.get(i)的大小。同样,arrayRecipe.get(j)应为getLength()

  3. 此外,getSize()(getLenght() - 1)确实从1开始计数而不是0,因此最终广告位的索引为<,但使用2687688为for循环中的子句处理。