Foreach vs普通的循环 [英] Foreach vs common for loop

查看:117
本文介绍了Foreach vs普通的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习Java,我碰到的第一件事是 foreach 循环,不知道它的工作方式,我做的第一件事是:

  int [] array = new int [10]; 
for(int i:array){
i = 1;

$ / code>

显然未能分配 1 到数组的每个元素。然后我把 System.out.print(i); (在 i = 1; 之后)添加到循环,看到屏幕的输出是 1111111111 但是因为在循环内部做 i 是有效的,可能 i 是数组的每个元素的副本,不是吗? (第一个问题)

如果以上是真的,那么这意味着 foreach 循环要慢得多常见的循环,因为它涉及复制数组的每个元素?或者由于 Java 没有指针和指针算术,所以 oprator [] 可能被设计成其他一些不好的

如果上面的假设是真实的,为什么要使用明显更慢的 foreach 循环而不是常见的 for 循环?



总之问题:




$ 数组的每个元素的副本?如果不是什么
那么?
  • 不是 foreach 常见的?如果不是这样,
    badly是如何设计的?

  • 除了在 foreach 循环中赢得可读性外,没有什么比这更简单的了。


    pre $ )) b

    您声明一个变量 i 循环迭代获取数组中下一个元素的值,它不是该元素的引用。



      i = 1; 

    给变量分配一个新值,而不是数组中的元素。

    不能直接使用foreach循环设置数组元素的值。对于(int i = 0),对于循环使用一个正常的循环对于那个

      ; i< array.length; i ++){
    array [i] = ...; //某些值





    在上面的例子中,你正在使用声明的变量 i 作为数组元素的索引。

      array [i ] 

    正在访问您可修改的元素本身。


    $ block $ $ $ b

    Ans显然未能将1赋给数组的每一个元素。 I
    添加了System.out.print(i);到循环的主体,看到屏幕的
    输出是1111111111,但是因为在循环内做了一些与
    的操作是有效的,所以我最有可能是每个元素
    的副本数组,不是吗? (第一个问题)

    您必须将 System.out.print(i)在$ i = 1 之后,否则您将得到 0000000


    如果以上情况属实,这并不意味着foreach循环比
    要慢很多,因为它涉及到每个
    的拷贝数组的元素?或者,因为Java没有指针和
    指针算术,oprator []可能被设计成其他
    糟糕的方式,复制每一个元素实际上更快?


    看看这里来看看foreach循环是如何工作的。对于数组,

      for (int i:array){
    i = 1;



    $ b $ p $相当于

    $ b

      for(int index = 0; index< array.length; index ++){
    int i = array [index];
    i = 1;
    }

    所以它不会变慢。你在堆栈上做了一个更原始的创建。



    这取决于实现。对于数组,它不会以任何方式变慢。它只是用于不同的目的。


    为什么要使用明显较慢的foreach循环而不是普通的
    forloop?


    一个原因是为了可读性。另一个是当你不关心改变数组的元素引用,而是使用当前引用。
    $ b

    以一个引用类型为例

      public class Foo {
    public int a;
    }

    Foo [] array = new Foo [3];
    for(int i = 0; i< array.length; i ++){
    array [i] = new Foo();
    array [i] .a = i * 17;


    (foo foo:array){
    foo.a = 0; //在每个Foo对象中设置`a`的值
    foo = new Foo(); //创建新的Foo对象,但不会替换数组
    }

    使用原始类型这样的东西不起作用。

      for(int index = 0; index< array.length; index ++){
    int i = array [index];
    i = 1; //不改变数组[index]
    }


    I just started learning Java and the first thing I came across is the foreach loop, not knowing the way it works the first thing I did was:

    int[] array = new int [10];
    for (int i: array){
        i = 1;
    }
    

    And obviously failed to assign 1 to every element of the array. Then I added System.out.print(i); (after i = 1;) to the body of the loop and saw that the output of the screen was 1111111111 but since doing something with i inside the loop is valid that most likely i is a copy of every element of the array, ain't it? (first questions)

    If the above is true doesn't this mean that the foreach loop is much slower then the common for loop since it involves making copies of each element of the array? Or since Java doesn't have pointers and pointer arithmetic, the oprator[] may be designed in some other "badly" fashion that copying every element is actually faster?

    And if the above assumptions are true, why one would use an obviously slower foreach loop instead of a common forloop?

    In short the questions:

    • Is i the copy of each element of the array? If not what is it then?

    • Isn't the foreach loop slower then the common one? If not, how "badly" is then operator[] designed?

    • There is nothing more except readability to win in a foreach loop?

    解决方案

    In the code

    for (int i: array){
    

    You declare a variable i that on each loop iteration gets the value of the next element in the array, it isn't a reference to that element.

    In

    i = 1;
    

    you assign a new value to the variable, not to the element in the array.

    You cannot set the values of array elements with a foreach loop directly. Use a normal for loop for that

    for (int i = 0; i < array.length; i++) {
        array[i] = ...; // some value
    }
    

    In the above example, you are using the declared variable i as an index to the element in the array.

    array[i] 
    

    is accessing the element itself whose value you can modify.


    Ans obviously failed to assign 1 to every element of the array. The I added System.out.print(i); to the body of the loop and saw that the output of the screen was 1111111111 but since doing something with i inside the loop is valid that most likely i is a copy of every element of the array, ain't it? (first questions)

    You must have put the System.out.print(i) after the i = 1, otherwise you would get 0000000.

    If the above is true doesn't this mean that the foreach loop is much slower then the common for loop since it involves making copies of each element of the array? Or since Java doesn't have pointers and pointer arithmetic, the oprator[] may be designed in some other "badly" fashion that copying every element is actually faster?

    Have a look here to see how the foreach loop works. For arrays,

    for (int i: array){
        i = 1;
    }
    

    is equivalent to

    for (int index = 0; index < array.length; index++) {
        int i = array[index];
        i = 1;
    }
    

    So it isn't slower. You're doing one more primitive creation on the stack.

    It depends on the implementation. For arrays, it's not slower in any way. It just serves different purposes.

    why one would use an obviously slower foreach loop instead of a common forloop?

    One reason is for readability. Another is when you don't care about changing the element references of the array, but using the current references.

    Take a reference type example

    public class Foo {
        public int a;
    }
    
    Foo[] array = new Foo[3];
    for (int i = 0; i < array.length; i++) {
        array[i] = new Foo();
        array[i].a = i * 17;
    }
    
    for (Foo foo : array) {
        foo.a = 0; // sets the value of `a` in each Foo object
        foo = new Foo(); // create new Foo object, but doesn't replace the one in the array
    }
    

    With primitive types such a thing doesn't work.

    for (int index = 0; index < array.length; index++) { 
        int i = array[index];
        i = 1; // doesn't change array[index]
    }
    

    这篇关于Foreach vs普通的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆