好的,所以我正在编写这个程序,其中数组的一部分设置为数组另一部分的值。相关代码是(顺便说一下是第50行):
variables[(program[1])+2]=inputs[0];
这里是数组初始化的地方:
int[] variables;
variables=new int[10000];
程序数组:
int[] program;
program=new int[10000];
输入数组:
int[] inputs;
inputs=new int[9];
当我运行它时,会产生错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Central.main(Central.java:50)
有什么想法吗?提前谢谢。
好的,这是整个代码:
import java.util.Random;
import java.util.Scanner;
public class Central {
public static void main(String[] args) {
int[] inputs;
inputs=new int[9];
int[] outputs;
outputs=new int [9];
int[]results;
results=new int [10000];
int[] program;
program=new int[10000];
int[][] random;
random=new int[100][100];
int[] bestProgram;
bestProgram=new int[10000];
int[] bestVariables;
bestVariables=new int [10000];
int[] variables;
variables=new int[10000];
int[][] bestRandom;
bestRandom=new int [100][100];
Scanner sc = new Scanner(System.in);
inputs[0]=sc.nextInt();
inputs[1]=sc.nextInt();
inputs[2]=sc.nextInt();
inputs[3]=sc.nextInt();
inputs[4]=sc.nextInt();
inputs[5]=sc.nextInt();
inputs[6]=sc.nextInt();
inputs[7]=sc.nextInt();
outputs[0]=sc.nextInt();
outputs[1]=sc.nextInt();
outputs[2]=sc.nextInt();
outputs[3]=sc.nextInt();
outputs[4]=sc.nextInt();
outputs[5]=sc.nextInt();
outputs[6]=sc.nextInt();
outputs[7]=sc.nextInt();
program=createProgram(program);
random=createRandom(random, program);
for (int g=0;g<=100;g++) {
random=createRandom(random,program);
variables=new int [program[1]+2];
variables[program[1]+1]=1;
results[0]=run (program,variables,random);
variables[(program[1])+2]=inputs[0];
results[1]=run (program,variables,random);
variables[program[1]+2]=inputs[1];
results[2]=run (program,variables,random);
variables[program[1]+2]=inputs[2];
results[3]=run (program,variables,random);
variables[program[1]+2]=inputs[3];
results[4]=run (program,variables,random);
variables[program[1]+2]=inputs[4];
results[5]=run (program,variables,random);
variables[program[1]+2]=inputs[5];
results[6]=run (program,variables,random);
variables[program[1]+2]=inputs[6];
results[7]=run (program,variables,random);
variables[program[1]+2]=inputs[7];
bestProgram=findBestProgram(program, variables, random, bestProgram, bestVariables, bestRandom, inputs, outputs, results);
}
for (int i=0;i<=program[0];i++)
{
System.out.println(bestProgram[i]);
}
}
public static int[] createProgram(int[] program) {
program[0]=randInt(1,50);
program[1]=randInt(1,3);
for (int i=2;i<=program[0];i++)
{
program[i]=randInt(1,3);
}
return program;
}
public static int run(int[] program, int[] variables,int[][] random)
{
for (int i=2;i<=program[0];i++) {
switch (program[i]) {
case 1: variables[random[i][0]]=variables[random[i][1]]+variables[random[i][2]];
break;
case 2: variables[(random[i][0])]=(variables[(random[i][1])])-(variables[random[i][2]]);
break;
default: System.out.println("This error really shouldn't happen ever");
}
}
return variables[0];
}
public static int[][] createRandom(int[][] random, int[] program)
{
for (int i=0;i<=program[0];i++)
{
for (int a=0; a<=2;a++)
{
random[i][a]=randInt(1,3);
}
}
return random;
}
public static int[] findBestProgram(int[] program, int[] variables, int[][] random,int[] bestProgram, int[]bestVariables, int[][]bestRandom, int[] inputs, int[] outputs, int[] results)
{
int bestProgramResult=run(bestProgram, bestVariables,bestRandom);
int[] resultz;
resultz= new int [10000];
resultz[0]=Math.abs(results[0]-outputs[0]);
resultz[1]=Math.abs(results[1]-outputs[1]);
resultz[2]=Math.abs(results[2]-outputs[2]);
resultz[3]=Math.abs(results[3]-outputs[3]);
resultz[4]=Math.abs(results[4]-outputs[4]);
resultz[5]=Math.abs(results[5]-outputs[5]);
resultz[6]=Math.abs(results[6]-outputs[6]);
resultz[7]=Math.abs(results[7]-outputs[7]);
int programResult=results[0]+results[1]+results[2]+results[3]+results[4]+results[5]+results[6]+results[7];
if (programResult<bestProgramResult)
{
bestProgramResult=programResult;
for (int i=0;i<=program[0];i++)
{
bestProgram[i]=program[i];
bestVariables[i]=variables[i];
bestRandom[i]=random[i];
}
}
return bestProgram;
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
//1: _=_+_
//2: _=_-1
答案 0 :(得分:0)
您收到异常是因为您尝试访问的索引不受数组大小的限制。换句话说,program[1] + 2
大于9999
,这是变量数组中最大的索引号。
答案 1 :(得分:0)
问题出在以下几行:variables=new int [program[1]+2];
基本上,如果我理解的是正确的,你已经重新初始化变量数组并将其大小更改为5.然后,在第50行:variables[(program[1])+2]=inputs[0];
你试图访问数组的第5个索引数组由0到4之间的索引定义。