?
Solved

Object cloning

Posted on 2003-03-06
3
Medium Priority
?
228 Views
Last Modified: 2010-03-31
I am trying to write a class which takes an array of objects(can be any type of object, even user defined classes) and returns a sorted copy of the array.  In my sort function, I am trying to clone the Object array using the lines of code:

//Clone the Original array
Object[] SortedArray = new Object[arry_of_objects.length];


//Clone the individual objects in the array into
//the new array  
for (int i = 0; i < array_of_objects.length; i++)
    SortedArray [i] = (Object) arry_of_objects[i].clone();
     

   But it will not let me do this since the clone() method is protected.  So...How can I go about cloning the inidivual objects into a new array?  Thanks
0
Comment
Question by:walker222
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Expert Comment

by:allahabad
ID: 8084854
The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

Create a class and implement Cloneable interface.
for ex.
public class Test implements  Cloneable{


    protected Object clone() {
       super.clone();
    }
}

Then your code :

Object[] SortedArray = new Test[arry_of_objects.length];


//Clone the individual objects in the array into
//the new array  
for (int i = 0; i < array_of_objects.length; i++)
   SortedArray [i] = (Test) arry_of_objects[i].clone();

0
 
LVL 3

Expert Comment

by:allahabad
ID: 8084942

Test[] SortedArray = new Test[arry_of_objects.length];


//Clone the individual objects in the array into
//the new array  
for (int i = 0; i < array_of_objects.length; i++)
  SortedArray [i] = arry_of_objects[i].clone();

0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 8084949
Just try:

Object[] SortedArray = (Object[]) arry_of_objects.clone();

You'll need to ensude that all the objects in your array are cloneable.



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question