Моя проблема в том, что я продолжаю получать ошибку в заголовке во время отладки.array [i] = java.lang.IndexOutOfBoundsException: Недопустимый диапазон массивов от 10 до 10
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;
}
}
}
Можете ли вы разместить раздел своего кода, в котором вы объявили массивы? – UnknownOctopus
sry забыл эту часть - добавил! – awefawe
Получаете ли вы это исключение * только * во время отладки? Код, показанный вами, кажется, работает отлично для меня. – Marvin