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 can you create a deep copy of an integer array in Java? | ComputerScienceOne | Bifalgorithm | Bifalgorithm