Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

array for each loop problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys!!
below is my program....i wonder why the output is 0030


public class testscjp {


public void myfunc()
{
int []arr={1,2,3,4};

for(int i:arr)
{

arr[i]=0;
}
for(int i :arr)
{
System.out.println(i);
}
}

public static void main(String args[])
{
testscjp tcjp = new testscjp();
tcjp.myfunc();

}

}

any help is greatly appreciated!!!
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it should be 0000. what you want to do with this prog?
 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also suspected it to be 0000....but look at the s.o.p....it prints the value of i and not the value of arr[i]
 
Marshal
Posts: 79635
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When there is "testscjp" in the name of the file, we need to know where it came from. Is that an exam question?
 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
testscjp is the name of the file which i made in my program...this question is from the mock exam....
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kunal Choudhary wrote:testscjp is the name of the file which i made in my program...this question is from the mock exam....



You need to tell us which mock exam, or the thread will be deleted. Furthermore, I'm going to move this to the SCJP forum.
 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from deepak bala's mock exam...also does it matter if the question is from mock exam....it's a java question rite??? it's your wish if you want to move it to scjp thing....but do keep me posted....what if i had not named the class scjptest???
 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am only concerned with the solution...however, i would keep in mind where to post the question from the next time
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

also does it matter if the question is from mock exam....it's a java question rite???



Yes, it matters. Some "mocks" violates copyrights -- ie. posting them would be illegal.

it's your wish if you want to move it to scjp thing....but do keep me posted....



It's already done. Notice that you are now in the SCJP forum.

what if i had not named the class scjptest???



If you were unable to tell us which mock, then this topic would have been deleted.

Henry
 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i please get a reply to my question....if the moving around thing is over.....i was impressed when you said ."It would have been deleted"....
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think what the value of i will be each time thru your first loop and the answer should become clear.
 
Kunal Choudhary
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks neal....i got it....
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't get it. please explain.

thanks in advance
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding the following s.o.p and check you can understand how it is setting the same array value a[1],a[3]

for(int i:arr)
{
System.out.println("i Value "+ i+ "Inside set array loop"+ arr[i]);
arr[i]=0;
}

Hope this helps...
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question has been asked a lot of time earlier

https://coderanch.com/t/427832/Programmer-Certification-SCJP/certification/Enhanced-Loop-Array

https://coderanch.com/t/418167/Programmer-Certification-SCJP/certification/Array

https://coderanch.com/t/418029/Programmer-Certification-SCJP/certification/Arrays
....

Please do some searching on the forum before you ask a question...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Ala, please change your name to meet the javaranch naming policy...
reply
    Bookmark Topic Watch Topic
  • New Topic