Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

copy an array

I am passing an object array into a function and I want to return a sorted copy of the array without altering the original.  I dont want to copy the actual objects in the array, I just need two arrays pointing to the same objects, just in different orders.  So the original array will remain unchanged, but the sorted array will point to the same objects at the one I passed into the function, but it will point to them in a sorted manner.  How do I go about creating and assinging the array which will hold the sorted order?  I already have an effective sorting algorithm, I just need to figure out how to define and assign the sorted array such that it will point to the same objects, yet when I run the sorting process, it will not alter the original array as well.  Thanks
0
baseballfan45
Asked:
baseballfan45
  • 2
1 Solution
 
ilmtCommented:
java.lang.System.arraycopy is an efficient way to make a copy of an array. The copy will be a separate array, but pointing to the same objects. (You are copying the references to the objects) You can then move the objects around in the copied array (eg foo[3] = x;) without affecting the original array.

0
 
baseballfan45Author Commented:
i tried that and that still passes back the unsorted array though for some reason.
0
 
baseballfan45Author Commented:
never mind...that worked
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now