does anyone know how can I print out the array that is inside lecArray[0] which is 1A / 01 1A / 02?
public class LecturerUser {
public static void main(String[] args) {
Lecturers[] lecArray = new Lecturers[4];
lecArray[0] = new FullTimeLecturer(
"John", 1, new String[]{"1A / 01", "1A / 02"},
new int[]{20, 22}, 3500);
lecArray[1] = new FullTimeLecturer(
"Jack", 2, new String[]{"1B / 01", "1B / 02"},
new int[]{25, 22}, 4000);
lecArray[2] = new PartTimeLecturer("Joe", 11, 60, 60);
lecArray[3] = new PartTimeLecturer("Janny", 12, 60, 45);
for (int i = 0; i < lecArray.length; i ) {
System.out.println(lecArray[i].getName() "'s monthly pay is " lecArray[i].calcMonthlyPay());
}
}
}
CodePudding user response:
You should create toString() function for Lecturers class. in the function, you print what you want show, and in main function, you only call lecArray[i].toString()
CodePudding user response:
Java is an Object Oriented language; what you want is not generally possible. Any object is a black box: Anything you want to do with it depends entirely on what the class definition that defines what you can do with instances of that class (those'd be the objects).
Fortunately, if it's in any way important, these class definitions tend to have methods defined that get you the information you wanted. For example, this may work:
System.out.println(Arrays.toString(lecArray[0].getResponsibleFor());
Here I am assuming that "1A / 02" stuff is groups they are responsible for.
Note:
Arrays are not appropriate for this kind of thing, they are a low-level constructs you use only when working with APIs that require them, interacting with the OS or internal java libraries, or are writing core utilities (e.g. ArrayList in its source uses an array). Arrays are mutable and can't be made immutable which is a big problem here, and its implementations of toString
, equals
, and hashCode
are mostly useless, and cannot be changed. That's why you should be replacing these with List<String>
instead. Or even better, List<Course>
, or Group, or whatever that is supposed to represent.
CodePudding user response:
you actually need another for loop in your case
for (int i = 0; i < lecArray.length; i ) {
System.out.println(lecArray[i].getName() "'s monthly pay is " lecArray[i].calcMonthlyPay());
for (int j = 0; j < lecArray[i].innerArray.length; i ){
System.out.println(lecArray[i].innerArray[j]);
}
}