0

I implemented compareTo in my Student class which implements Comparable which has a private int age field. I also have a Classroom class which has a classroom array that takes in Student objects. I have my classroom array and I want to send it's individual int age fields into a new Student array. How would I do that because referencing doesn't work with array indexes in a for loop for example:

 for (int i = 0; i < classroom.getSize(); i++)
        {
            studentArray[i] = classroom[i].getAge();
        }

Assuming my getters are correct and and arrays are declared properly, this declaration doesn't work because it's two different types of objects. What are the alternatives?

mobcity zkore
  • 263
  • 1
  • 8
  • 2
    Please post a MCVE. I can't tell what you're asking. Your code doesn't use `compareTo`. – Elliott Frisch Sep 18 '18 at 01:28
  • It's an override method I wrote in student class and it's not needed for what i'm doing. I did it to have Student implement comparable and use Arrays.sort() – mobcity zkore Sep 18 '18 at 01:33
  • 1
    [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Elliott Frisch Sep 18 '18 at 01:34
  • 1
    It's pretty clear why your code doesn't work, but it's not clear at all what you're trying to do. – shmosel Sep 18 '18 at 02:02
  • As far as I have understood your problem, you want to set `Integer` from one class to another. You can call the `setAge()` method for student class. You can do something like this: `studentArray[i].setAge(classroom[i].getAge());` – Mushif Ali Nawaz Sep 18 '18 at 02:24

0 Answers0