array [i] = java.lang.IndexOutOfBoundsException:无效的数组范围10到10

时间:2015-06-29 22:08:23

标签: java android arrays random indexoutofboundsexception

我的问题是我在调试期间一直在标题中收到错误。

array[i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10

以下是for循环。我输入了值people = 10, payer = 4, rest = 6

int[] array = new int[people];
Random rand = new Random();
int rest = people-payer;

for(int i=0; i<people; i++) {
    if(payer<=0) { /*Payers are already decided*/
        array[i] = 0;
        continue;
    }
    if(rest<=0) {
        array[i] = 1;
        continue;
    }
    else {
        int randomNumber = rand.nextInt(2);
        array[i] = randomNumber;
        if (randomNumber == 1)
            payer--;
        if (randomNumber == 0)
            rest--;
    }
}

我认为错误在于我试图访问我的数组的索引10并不存在。我不知道我在哪里访问数组[10]。或者问题可能在别的地方?

感谢您的时间:)

+加成

这是整个代码,但它已经很长时间了......我不知道它是否会有所帮助。 for循环位于onClick方法中。

package activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chloe.myapplication.R;

import java.util.Random;

public class PayActivity extends ActionBarActivity implements View.OnClickListener {
    Button submitButton, flip;
    Switch switchButton;
    EditText et_totalPeople, et_payPeople, et_amount;
    TextView tv_payPeople, tv_people;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_numofpeople);

        submitButton = (Button) findViewById(R.id.submitButton);
        switchButton = (Switch)findViewById(R.id.switchButton);
        et_totalPeople= (EditText) findViewById(R.id.et_totalPeople);
        et_payPeople= (EditText) findViewById(R.id.et_payPeople);
        et_amount = (EditText) findViewById(R.id.et_amount);

        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked == true) {
                    /*After entering a value in the first editText and pressing enter,
                     * the qwerty keyboard comes up and won't move to third editText even though I push enter...SOLVE */
                    et_payPeople.setEnabled(false);
                    et_payPeople.setClickable(false);
                } else {
                    et_payPeople.setEnabled(true);
                    et_payPeople.setClickable(true);
                }
            }
        });
        et_totalPeople.setSelectAllOnFocus(true);
        et_payPeople.setSelectAllOnFocus(true);
        et_amount.setSelectAllOnFocus(true);
        submitButton.setOnClickListener(this);
    }
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.submitButton:
                int people = Integer.parseInt(et_totalPeople.getText().toString());
                if(people<1 || people>100) {
                    Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
                    break;
                }
                int amount = Integer.parseInt(et_amount.getText().toString());
                if(amount<1 || amount>10000000) {
                    Toast.makeText(getApplicationContext(), "Enter values 1~10,000,000", Toast.LENGTH_LONG).show();
                    break;
                }
                /*Only certain people pay*/
                if(switchButton.isChecked()==false) {
                    int payer = Integer.parseInt(et_payPeople.getText().toString());
                    if(payer<1 || payer>100) {
                        Toast.makeText(getApplicationContext(), "Enter values 1~100", Toast.LENGTH_LONG).show();
                        break;
                    } /*All three values are valid: SHOW CARDS*/
                    else {
                        int[] array = new int[people];
                        Random rand = new Random();
                        int rest = people-payer;
                        for(int i=0; i<people; ++i) {
                            /*Payers are already decided*/
                            if(payer<=0) {
                                array[i] = 0;
                                continue;
                            }
                            if(rest<=0) {
                                array[i] = 1;
                                continue;
                            }
                            else {
                                int randomNumber = rand.nextInt(2);
                                array[i] = randomNumber;
                                if (randomNumber == 1)
                                    payer--;
                                if (randomNumber == 0)
                                    rest--;
                            }
                        }
                        AlertDialog.Builder dialog = new AlertDialog.Builder(PayActivity.this);
                        dialog.setView(R.layout.card_questionmark);
                        for(int i=0; i<people; i++) {
                            /*PASS*/
                            if(array[i] == 0) {
                                dialog.setView(R.layout.card_questionmark);
                                dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        new AlertDialog.Builder(PayActivity.this)
                                        .setView(R.layout.card_pass)
                                        .setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int which) {
                                                finish(); /*move on to next value in array*/
                                            }
                                        });
                                    }
                                });
                                /*If not the first card, show previous card*/
                                if(i!=0) {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                       public void onClick(DialogInterface dialog, int which) {
                                           finish();
                                       }
                                    });
                                    i--; /*return to previous value in array*/
                                } /*First card*/
                                else {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }
                            }/*YOU PAY*/
                            else {
                                dialog.setView(R.layout.card_questionmark);
                                dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        new AlertDialog.Builder(PayActivity.this)
                                            .setView(R.layout.card_youpay)
                                            .setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int which) {
                                                    finish(); /*move on to next value in array*/
                                                }
                                            });
                                    }
                                });
                                /*If not the first card, show previous card*/
                                if(i!=0) {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            finish();
                                        }
                                    });
                                    i--; /*return to previous value in array*/
                                } /*First card*/
                                else {
                                    dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
                                        }
                                    });
                                }
                            }
                        }
                    }
                } /*Everyone Pays*/
                else {

                }
            break;
        }
    }
}

1 个答案:

答案 0 :(得分:-5)

我可以告诉你,如果你的数组中有10个或更多的空间,它就不会给你这个错误,因为这个错误说你走出了你声明这个数组会得到的空间。而这个循环只运行了10次。 尝试调试它。