Saturday, May 28, 2011

Using a Variable as your index in java

Using a Variable as your index in java

sample code:

int x[] = new int [3];

int index;
index = 0;

x [index]= 1; //this places 1 into x[0].. 

index = 2;

x[index] = 10; //this places 10 int x[2]..

index = 3;
x[index] = 20; 
//this is an error.. because index 3 is already out of bounds. because you declare x with 3 slots, you are only limited until x[2]..

No comments: