java - How does this pre increment on arrays work -


++frequency[2] ; 

i thought default value of array hasn't been initialized 0. imo, means evaluate frequency[2] 0 , add 1 , assign index 2. pls can explain how output code gotten. been hitting head real hard on this.

 public static void main(string ars[]) {      int responses[] = {1,2,4,4};    int freq[] = new int[5];     for(int answer = 1;answer < responses.length;answer++){         ++freq[responses[answer]];     }     ( int rating = 1; rating < freq.length; rating++ )          system.out.printf( "%6d%10d\n", rating, freq[ rating ] ); 

//output

     1         0      2         1      3         0      4         2 

yes, are.

int[] frequency = new int[3]; system.out.println(frequency[2]); system.out.println(++frequency[2]); system.out.println(frequency[2]); 

prints:

0 1 1 

edit: lurker said here how pre increment on arrays work, answer valid java, might not other languages.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -