• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

arrays and for each

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code gives [0,o,3,0]as answer
can any body explains the reason

import java.util.*;
class test
{
public static void main( String args[] )
{
int []arr = {1,2,3,4};
for ( int i : arr )
{
arr[i] = 0;
}
System.out.println(Arrays.toString(arr));

}
}
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code gives me

ArrayIndexOutOfBoundsException: 4 error!
 
Sheriff
Posts: 9707
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 too many times. Search it to find a solution. I think it's time to put it in the FAQ. Here's a bit of searching that I did

https://coderanch.com/t/418167/java-programmer-SCJP/certification/Array

https://coderanch.com/t/418029/java-programmer-SCJP/certification/Arrays

https://coderanch.com/t/417661/java-programmer-SCJP/certification/integer-array

https://coderanch.com/t/270798/java-programmer-SCJP/certification/John-Meyer-SCJP-mock-exam
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think it's time to put it in the FAQ.


Feel free to do so - the FAQ is editable by everyone.
 
Ankit Garg
Sheriff
Posts: 9707
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

Originally posted by Ulf Dittmer:

Feel free to do so - the FAQ is editable by everyone.



Thanks Ulf. I never saw the Edit this page button on the bottom. I'll surely add this question to the FAQ...
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really a nice question guys..

Thanks for the help.
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really a nice question guys..

Thanks for the help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic