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

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

最佳答案

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

关于java - 数组 [i] = java.lang.IndexOutOfBoundsException : Invalid array range 10 to 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126401/

相关文章:

java - 通过java中的命令行参数从文件读取数据,文件名密码

Java Apache HttpComponents,如何读取 POST 响应?

java - 如何将自定义 java 类导入到我的 Antlr 语法中?

javascript - HTML5音频开始播放延迟

c - 在 C 中使用非标准的数组声明

php - 从 session 数组生成逗号分隔的列表

java - Log4j 在所有类级别记录

android - 如何让 Overview Screen 中的任务看起来像 Chrome 一样?

android - "You cannot install this app because another user has already installed an incompatible version on this device"

javascript - 返回 firebase 数组返回元组而不是数组