title: Copying Arrays The Right Way
body: <p>I came across 2 examples from my notes which is about copying arrays.</p>
  
  <p>The first example given below, stated that it is not the way to copy an array. But, when i tried to run the code, it managed to copy all the values from array1 to array2.</p>
  
  <pre><code>    int []array1={2,4,6,8,10};
      int []array2=array1;
      for(int x:array2){
          System.out.println(x);
      } 
  </code></pre>
  
  <p>The second example given, was saying the right way to copy an array.</p>
  
  <pre><code>int[] firstArray = {5, 10, 15, 20, 25 };
  int[] secondArray = new int[5];
  for (int i = 0; i &lt; firstArray.length; i++)
    secondArray[i] = firstArray[i];
  </code></pre>
  
  <p>My question is, are these 2 examples appropriate to be applied in coding or Example 2 is preferred. If you were my lecturer, I were to apply Example 1.. I will be given less mark compared to Example 2 method or just the same?</p>