aspose file tools*
The moose likes Beginning Java and the fly likes Initializing and (later) accessing arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializing and (later) accessing arrays" Watch "Initializing and (later) accessing arrays" New topic
Author

Initializing and (later) accessing arrays

Jim Anderson
Greenhorn

Joined: Apr 19, 2013
Posts: 15

//I have a major problem with initializing and (later) accessing arrays. I start with an applet declaration; e.g.,





Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In this line



where do you actually assign the references to refer to arrays?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the ranch.

Please use code tags as follows:



Looks better doesn't it?


~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Which IDE are you using?
Jim Anderson
Greenhorn

Joined: Apr 19, 2013
Posts: 15

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

Joined: Oct 02, 2003
Posts: 11498
    
  16

Please learn to UseCodeTags (<-click) when posting source code.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initializing and (later) accessing arrays