Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing and (later) accessing arrays

 
Jim Anderson
Greenhorn
Posts: 15
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//I have a major problem with initializing and (later) accessing arrays. I start with an applet declaration; e.g.,





 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this line



where do you actually assign the references to refer to arrays?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

Please use code tags as follows:



Looks better doesn't it?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which IDE are you using?
 
Jim Anderson
Greenhorn
Posts: 15
Eclipse IDE Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot 3 lines in my earlier post. My init() file should read:

public void init(){
//... followed by array initialization
int delta_x[] = new int[numDeltas];
int delta_y[] = new int[numDeltas];
int alpha[] = new int[numDeltas];

int delta_x[]= new int[numDeltas];
int delta_y[]= new int[numDeltas];
int alpha[]= new int[numDeltas];

for(int kk=0;kk<numDeltas;kk++){
delta_x[kk]=1;
delta_y[kk]=1;
alpha[kk]=1;
myRan = Math.random();
if(myRan>0.5){delta_x[kk]=-1;}
myRan = Math.random();
if(myRan>0.5){delta_y[kk]=-1;}
// end kk loop
}
// end init()
 
fred rosenberger
lowercase baba
Bartender
Posts: 12124
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please learn to UseCodeTags (<-click) when posting source code.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe your issue is shadowing. You have the three arrays declared as instance variables. But in the init method, you create local variables with the same name. The instance variables are still null.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: . . . Looks better doesn't it?
Not a lot better. Not even when I added code tags to the original post.

That code had not been correctly indented; there is too much writing crammed into too small a space. Please spread it out, Jim Anderson, so we can actually read it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic