Arrays and enhanced for loops?

后端 未结 6 557
闹比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:31

    Here in the advanced for loop, the int j does not represent the index. Rather it represents the values in the xy[] array. It is not possible to get the index of advanced for loop. If you want index, then you might have to use ordinary for loop. Ex. If you have

    xy[]={5,8,3,4};
    for(int j:xy)
    {
        println(j);
    }
    

    then the output would be

    5
    8
    3
    4
    

提交回复
热议问题