Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 23rd, 2006, 2:16 AM   #11
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
hrmph, can't edit my posts...

umm, I have another technical question...

I had a bunch of processes which I wanted to repeat, so I put them in a while loop, however I kept on getting an 'array index out of bounds' exception, I was stuck on it for a long time last night, trying to find the problem. Today I was talking about it with someone at uni and he suggested I change the while loop to another kind of loop, I just changed it to a for loop, and it worked.

Is there something in while loops other than what they do that was causing the exception? Or am I just going insane, and sleep and some time off from the computer helped me get the thing to work...
tumbleTetris is offline   Reply With Quote
Old May 23rd, 2006, 4:21 AM   #12
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 378
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Well , it depends on how you set up your while. It all comes down to the condition you put in while(condition) {}.

I don't see why it wouldn't work with while.

You have in a previous example

while ( x < 26;)
{
System.out.print(correct[x]);
}
now, if you don't increase x, it will always be smaller than 26 . So next time:
while ( x < 26;)
{
System.out.print(correct[x]);
x++;
}
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old May 23rd, 2006, 9:44 AM   #13
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
About your x y question on int variables.
If you have x = 5, y = 10
then do x = y;
x = 10, y = 10,
now if you do y = 15;
x = 10, y = 15
Basicly these types of variables (primitives) store there value with the variable name in memory.
Objects on the other hand point to there values(referenced),
so if you have
obj x = 'ex1', obj y='ex2'
x = y (this makes them point to the same place in memory)
x = 'ex2', y = 'ex2'
Any change made to one, changes the other.
x = 'ex1'
x = 'ex1' y = 'ex1'
NSchnarr is offline   Reply With Quote
Old May 23rd, 2006, 12:53 PM   #14
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by NSchnarr
Objects on the other hand point to there values(referenced),
so if you have
obj x = 'ex1', obj y='ex2'
x = y (this makes them point to the same place in memory)
x = 'ex2', y = 'ex2'
Any change made to one, changes the other.
x = 'ex1'
x = 'ex1' y = 'ex1'
This is incorrect.

In your example, x = y makes x and y point to the same object in memory, not the same place in memory. In additon, setting one of the variables to a new object will not affect the other as they no longer point to the same object in memory.

Any changes made to the object that both x and y are pointing to after assigning x = y will be reflected in both variables (for example, x.setAttribute(newvalue)), but reassigning one of the variables to another object (for example, x = new Object()) will not affect the other.

You would benefit from reading the article I referred to in a previous post, Java is Pass-By-Value, Dammit!.
titaniumdecoy is offline   Reply With Quote
Old May 23rd, 2006, 2:30 PM   #15
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 750
Rep Power: 3 Jimbo is on a distinguished road
IIRC (I didn't read the article so I don't know if it covered this), Java will copy the value of primitives, but then do as you mentioned with Objects. So for the example using ints, changing one will not affect the other.
Jimbo is offline   Reply With Quote
Old May 23rd, 2006, 3:10 PM   #16
NSchnarr
Newbie
 
Join Date: May 2006
Posts: 28
Rep Power: 0 NSchnarr is on a distinguished road
@titaniumdecoy
You are right, and much better at wording what was going through my head. lmao.
Ignore my post
NSchnarr is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC