Concept
How can you create a deep copy of an integer array in Java?
ComputerScienceOne / Multidimensional Arrays
"We can also create an array in a method and return it as a value. For example, the following method creates a deep copy of the given integer array. That is, a completely new array that is a distinct copy of the old array. In contrast, a shallow copy would be if we simply made one reference point to another reference.\n\n/**\n\n* This method creates a new copy of the given array\n\n* and returns it.\n\n*/\n\npublic static int [] makeCopy(int a[]) {\n\nint copy[] = new int[a.length];\n\nfor(int i=0; i<n; i++) {\n\ncopy[i] = a[i];\n\n}\n\nreturn copy;\n\n}\n\nThe method returns an integer array. In fact, this method is so useful, that it is already provided as part of the Java Software Development Kit (SDK). The class Arrays contains dozens of utility methods that process arrays. In particular there is a copyOf() method that allows you to create deep copies of arrays and even allows you to expand or shrink their size."
Related Ideas
- How do you create and populate a two-dimensional integer array?ComputerScienceOne · Dynamic Data Structures
- How does PHP support and create multidimensional arrays?ComputerScienceOne · Multidimensional Arrays
- How can arrays be passed to and returned from methods in Java?ComputerScienceOne · Dynamic Memory
- How can two indices be used to set values in a two-dimensional PHP array?ComputerScienceOne · Multidimensional Arrays
- How can you iterate over Java arrays?ComputerScienceOne · Basic Usage