Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop problem

 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

aren't both the for loops initialisng the array elements?
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the jdk1.5 for loop is used for read only purpose and not the changes the data or values.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks !!!
got it
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for each loop is used for iterating purpose, an advanced version of old for loop..
 
suresh pilakal babu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Geet,

In below for loop

for(Dog a og){
a=new Dog();
}//but if i use this one then the output is null

you are not initialising dog array, but you are assign the Dog object to the variable 'a'. thats why displaying 'null'.

if you want to initialse dog array you have modify like this

int i = 0;
for(Dog a og){
a=new Dog();
dog[i++] = a;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic