permaculture playing cards*
The moose likes Beginning Java and the fly likes Copy objects from one array over to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Copy objects from one array over to another" Watch "Copy objects from one array over to another" New topic
Author

Copy objects from one array over to another

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Im trying to search an array of the Students object type by their student ID (UFID). TAs2 is an array of GradStudent objects. GradStudent is a subclass of student. When I find the student in the Student array that has the same ID as the TA object, I need to add the matching Student object to the TAs array. When I try to run the program, it says TAs2 and uStudents are of incompatible types. How can I get around this error? Thank you for any help!

Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

As you said TAs2 is the array of GradStudent , what is the type of uSudents, they must be different types that why compiler is complaining..
Can you post the complete code. ?


Do not wait to strike till the iron is hot; but make it hot by striking....
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
My code for this project is very extensive. I have about 9 different classes, but I will attach the more important ones below. uStudents is of the Student type. Student is a super class for GradStudent.

Student:


GradStudent:


Clip of the University Class: (it is very long so I only posted the important bit)

Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

As you can see that TAs2 is of type GradStudent that is the sub class of Students and uStudents is the array of Students.

You can't refer an object of super class to sub class reference type.
so while
is correct
is incorrect
this is why you are getting error.
you are trying to store a super class type of object in sub class type reference.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Is there any way around this? For instance, is there a way to cast an object of a subclass of Class A to another subclass of Class A? Or can you cast a super class to a subclass type? Students is an abstract method so I can't create objects of the students type :/ The students in the array uStudent of the Student type must be either GradStudent or UndergradStudent, but then I have to search through the array of uStudents for the TAs for each course and store them in an array of the GradStudent type. I'm just so confused how I should go about this
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kd Martin wrote:Is there any way around this? For instance, is there a way to cast an object of a subclass of Class A to another subclass of Class A?


No. As someone pointed out recently, Lawnmowers and Alligators are both objects, but you can't cast one to the other. If you want to "convert" one to the other, you'll have to create a new object of the target type based on the original type. Usually this is done by passing a reference to the original to a factory method or to a target constructor.

Or can you cast a super class to a subclass type?


You can cast a supertype reference to a subtype if the object that reference points to is actually of that subtype.

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Great! Ok I think I've got that figured out. Now I am coming across this error:
University.java:352: variable instructor2 might not have been initialized
course = new Course(cType, cNum, cTitle, cNumCredits, instructor2, TAs2, cap);

If two objects are of the same type, and object A is null, can't I set A equal to B so that the reference A points to object B as I did below?

Here is the bit of code it refers to.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kd Martin wrote:Great! Ok I think I've got that figured out. Now I am coming across this error:
University.java:352: variable instructor2 might not have been initialized


The error message is telling you exactly what's wrong. On line 352 of University.java, where you're doing course = new Course(...), you're using a variable instructor2, but it's possible that you got there without giving that variable a value. In this case, if your for loop body never executes, or if the if condition is never true, you'll never have assigned a value to instructor2, so it will be undefined.

One approach would be to initialize instructor2 to null, and then check if it's still null after the loop. How to handle it really depends on what the requirements are if the instructor isn't found.

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 58
Great thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy objects from one array over to another