About 1,220,000 results
Open links in new tab
  1. copy a 2d array in java - Stack Overflow

    Nov 6, 2009 · There are two good ways to copy array is to use clone and System.arraycopy(). Here is how to use clone for 2D case: myInt[i] = matrix[i].clone(); For System.arraycopy (), you use: int[] aMatrix = matrix[i]; int aLength = aMatrix.length; myInt[i] = new int[aLength]; System.arraycopy(aMatrix, 0, myInt[i], 0, aLength);

  2. How do I copy a 2 Dimensional array in Java? - Stack Overflow

    To copy the content of an array to another array, use the for loop for(int i=0; i<old.length; i++) for(int j=0; j<old[i].length; j++) old[i][j]=current[i][j]; PS: For a one-dimensional array, you can avoid creating your own for loop by using Arrays.copyOf

  3. Array Copy in Java - GeeksforGeeks

    Nov 25, 2024 · In Java, copying an array can be done in several ways, depending on our needs such as shallow copy or deep copy. In this article, we will learn different methods to copy arrays in Java. Example: Assigning one array to another is an incorrect approach to copying arrays.

  4. java - Make copy of an array - Stack Overflow

    Arrays.copyOf(): If you want to copy first few elements of an array or full copy of array, you can use this method. Obviously it’s not versatile like System.arraycopy() but it’s also not confusing and easy to use.

  5. System.arraycopy() in Java - GeeksforGeeks

    Jun 27, 2022 · The java.lang.System.arraycopy () method copies a source array from a specific beginning position to the destination array from the mentioned position. No. of arguments to be copied are decided by an argument.

  6. How to Copy an Array in Java - Baeldung

    May 11, 2024 · This method takes the following arguments: a source array, the starting position to copy from the source array, a destination array, the starting position in the destination array, and the number of elements to be copied.

  7. Arrays copyOf() in Java with Examples - GeeksforGeeks

    Nov 11, 2024 · Arrays.copyOf() is a method of java.util.Arrays class. It is used to copy the specified array, truncating or padding with false (for boolean arrays) if necessary so that the copy has the specified length.

  8. How to Copy a 2D Array in Java - Delft Stack

    Feb 2, 2024 · In Java, the Arrays.copyOf() method provides a straightforward way to copy an array, including 2D arrays. This method creates a new array with a specified length and copies elements from the original array into the new array.

  9. Copy Array in Java [With Many Examples] - Know Program

    There are various in-built methods like System.arraycopy (), Arrays.copyOf (), Arrays.copyOfRange (), and clone () method which can be used to copy array in Java. We can also copy manually by assigning each element to another array element. Let us see them one by one. We can copy array in Java by using the assignment operator (=).

  10. Java Array Copy – Deep Copy and Shallow Copy - HowToDoInJava

    Feb 3, 2023 · Learn to create an array copy in Java. We will learn to shallow copy and deep copy an array with easy-to-follow examples. 1. Creating a Shallow Copy of Array. In shallow copying, the references of the array items are copied into the new array, so any change in the array or the array items is visible on the cloned copied array.

  11. Some results have been removed
Refresh