Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阵列和增强的for循环?_Java_Arrays - Fatal编程技术网

Java 阵列和增强的for循环?

Java 阵列和增强的for循环?,java,arrays,Java,Arrays,有人能解释一下这种方法是怎么回事吗 class test{ public static void main(String args[]) { int[] x = new int[4]; int[] xy = new int[4]; //System.out.println(xy[0]); for(int j : x) { xy[j] += 1; } System.out.println(xy[0]); }} 所以我认为增强的for

有人能解释一下这种方法是怎么回事吗

class test{  
public static void main(String args[])
{
   int[] x = new int[4]; 
   int[] xy = new int[4]; 
  //System.out.println(xy[0]); 
    for(int j : x) { 
       xy[j] += 1;
    }
   System.out.println(xy[0]); }}
所以我认为增强的for循环会这样做

/**for(int j=0; j < x.length(); j++)
xy[j=0] = 1
xy[j=1]=1
xy[j=2]=1
xy[j=3]=1*/
/**for(int j=0;j
但是从我所阅读的内容来看,增强的for循环正在为(int-element:array)执行
for
。不过,我不明白我的方法中的for循环实际上在做什么。我尝试了
System.out.println()
语句来检查数组
xy
中放入的内容,但它不是地址就是0

谢谢你的帮助,如果这让人困惑,我道歉

int[] x = new int[4]; 
这将创建一个包含4个元素的数组。每个元素的值都是0

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

这将遍历
x
的所有值。在每次迭代中,
j
x
中的下一个元素,但由于所有元素都初始化为0,因此j始终为0。因此,如果您使用调试器跟踪,那么4次迭代增量xy[0]

应该可以直接找到

简言之,它获取
x
中的每个值,并使用该值作为索引来增加
xy
中的相应值

如果使用有意义的值初始化两个数组,则更为明显:

int[] x = {1,1,3,3}
int[] xy = new int[4]; // initialized to 0 by default
在您的代码段之后,您将发现
xy
包含
{0,2,0,2}


您应该了解如何为阵列扩展增强for循环:

for (T v : arr) {
    // do something 
}
转化为

for (int i = 0; i < arr.length; ++i) {
    T v = arr[i];
    // do something 
}
for(int i=0;i

显然,您对如何扩展增强for循环的理解是错误的。

对于您在此处使用的每个循环,确实为j分配了一个已经分配给数组x的值(在您的示例中是0)

根据您对x和xy阵列的情况:-

x[0]=0;    xy[0]=0
x[1]=0;    xy[0]=0
x[2]=0;    xy[0]=0
x[3]=0;    xy[0]=0
根据您的程序案例描述每个循环:-

for(j : x)
  • 这意味着它将运行4次,即数组x的长度

  • 第一次运行时,将发生以下过程

    j=x[0] (so j=0 coz x[0] according to your case)
    xy[0]=xy[0]+1 (so now xy[0] value becomes 1)
    
  • 同样地 对于每个的第二次运行

    j=x[1] (so j=0 coz x[0] according to your case)
    xy[0]=xy[0]+1 (so now xy[0] value becomes 2 as in previous for each run xy[0]=1)
    
  • 总而言之,在第四次运行for-each循环结束时,您将得到
    xy[0]=4


  • 最后,print语句将打印4

    在高级for循环中,int j不表示索引。而是表示xy[]数组中的值。无法获取高级for循环的索引。若您想要索引,那个么您可能必须使用普通for循环。 如果你有

    xy[]={5,8,3,4};
    for(int j:xy)
    {
        println(j);
    }
    
    那么输出将是

    5
    8
    3
    4
    

    在您的情况下,您只需要for循环,而不需要增强for循环:

    for(int j = 0 ; x.length > j; j ++) { 
       xy[j] += 1;
    }
    
    代码的问题是循环只在xy的索引0处遍历,因此得到的xy为[4,0,0,0]

    int[] x = new int[4];//default values [0, 0, 0, 0]
    int[] xy = new int[4]; //default values [0, 0, 0, 0]
    for(int j : x) { // j is always 0
       xy[j] += 1;// xy[0] += 1
    }
    

    您可以改为使用for循环:

    for(int i = 0 ; x.length > i; i ++) { 
    xy[i] += 1;
    }
    
    如果您仍然希望为每个应用程序使用,那么下面是一个示例

    a[]={1,2,3,4};
    for(int i:a)
    {
    println(i);
    }
    

    希望有帮助

    这正常吗?因为我从未见过这样的事情,这样的for循环的目的是什么?@KickButtowski foreach循环的行为和一般用途是正常的。数组的类型(全零)和循环的内容(将第0个元素增加n倍)是。。。好。。。特别的。看起来它们来自某种测试。目标是对数组或集合的每个元素执行某些操作,这在大多数情况下都是对循环执行的操作:
    for(Employee e:employees){increaseSalary(e);}
    。@JBNizet k使一些非常新颖有趣的东西变得酷起来。你能不能和蔼可亲一点,把它再细分一点?你想按照你的“想法”来制作这个节目吗?