Skip to content
Advertisement

Object array with variable from another class

Worksheet Question:

The question said to declare an array of 5 objects of the class Node in the main Class – I managed to this as shown below

But then the question continues populate the array by objects with seqNo values assigned to 1,2,3,4,5 respectively. Then traverse the array and print the list of its object using method show()

I have an error when I am trying to show the array to the user. I am trying to display the array by this line of code:

JavaScript

Below I have all the code except the Person class. Does someone have any idea if I should do a loop. When i tried if loop I got an error too. I have the display part code wrong I can’t figure out what to change

My AnyClass

JavaScript

My Node class

JavaScript

MainProg

JavaScript

Advertisement

Answer

Below I have all the code except the Person class. Does someone have any idea if I should do a loop. When i tried if loop I got an error too. I have the display part code wrong I can’t figure out what to change

Yes, you need to loop over the array. At this level of instruction you should use a for or foreach loop.

JavaScript

Or

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement