• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Copy objects from one array over to another

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. ?
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great thanks!
 
eat bricks! HA! And here's another one! And a tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic