Arrays and enhanced for loops?

后端 未结 6 501
闹比i
闹比i 2021-01-22 15:19

Can someone please explain what is going on in this method?

class test{  
public static void main(String args[])
{
   int[] x = new int[4]; 
   int[] xy = new i         


        
6条回答
  •  悲哀的现实
    2021-01-22 15:38

    int[] x = new int[4]; 
    

    This creates an array of 4 elements. Each element's value is 0.

    for(int j : x) { 
       xy[j] += 1;
    }
    

    This iterates through all the values of x. At each iteration, j is the next element in x, but since all elements are initialized to 0, j is always 0. So, the 4 iterations increment xy[0].

提交回复
热议问题