简体   繁体   中英

Java Class - Array println

i have 2 Class, the first class College and the second Lecturer. in College class i have >> public Lecturer[] allLecturer;

I want the college department will receive the Lecturer department, But I have something strange when I try to print the college department.

class Lecturer:

public class Lecturer {
public String name;
public int numOfTimesPenFalls;
public String favoriteIceCream;
public int autoNumber;

//constructor
public Lecturer(String Name, int NumOfTimesPenFalls,
        String FavoriteIceCream, int AutoNumber) {
    this.name = Name;
    this.numOfTimesPenFalls = NumOfTimesPenFalls;
    this.favoriteIceCream = FavoriteIceCream;
    this.autoNumber = AutoNumber;
}

@Override
public String toString(){
    return "name: " +name+ " num Of Times Pen Falls: "+ numOfTimesPenFalls +
    " favorite Ice Cream: " + favoriteIceCream + " auto Number: " + autoNumber;
}
}

class College:

public class College {
public String name;
public int numOfLecturer;
public Lecturer[] allLecturer;
public int maxLecturer;

//constructor
public College(String Name, int NumOfLecturer, Lecturer[] AllLecturer, int MaxLecturer) {
    this.name = Name;
    this.numOfLecturer = NumOfLecturer;
    this.allLecturer = AllLecturer;
    this.maxLecturer = MaxLecturer;
}

public College(String Name){
    this.name = Name;
}

@Override
public String toString(){
    return "Name College: " +name+ " num Of Lecturer: " + numOfLecturer + 
    " all Lecturer: " + allLecturer + " max Lecturer " + maxLecturer ;
}
}

main:

public class main {
public static void main(String[] args) {

    Lecturer[] L1 = new Lecturer[] { new Lecturer("David", 3, "Banana",
            1001) };

    College myCollege = new College("College1", 20, L1, 10);

    System.out.println(myCollege.toString());

}
}

Result output:

Name College: College1 num Of Lecturer: 20 all Lecturer: [LLecturer;@139a55 max Lecturer 10

Why it prints me the ([LLecturer;@139a55) Instead the details of the department?

If I write in main for loop:

for (int i = 0; i < L1.length; i++) {
        System.out.println(L1[i]);
    }

the Result output:

 name: David num Of Times Pen Falls: 3 favorite Ice Cream: Banana auto Number: 1001

How do I fix this so that when I print the class College (System.out.println(myCollege.toString()) );
I also printed the information that is in the lecturer Department?

thank you.

hey when you concat an array with string it will use his toString function. the default toString function will print something like you see in your code. the toString of a single object in the array will print what you want .

Java arrays do not override Object 's toString() method. just add the loop that you wrote to the toString() method of College

example solution:

@Override
public String toString(){
    return "Name College: " +name+ " num Of Lecturer: " + numOfLecturer + 
    " all Lecturer: " + java.util.Arrays.toString(allLecturer) + " max Lecturer " + maxLecturer ;
}

and if you can use 3rd party library, take a look at Apache's StringUtils class, it has a handy join() method that produces a String from concatanating array of objects

change your toString() method in College.java to

@Override
public String toString(){
    return "Name College: " +name+ " num Of Lecturer: " + numOfLecturer + 
    " all Lecturer: " + Arrays.toString(allLecturer) + " max Lecturer " + maxLecturer ;
}

and in your main method try to print your College reference

College myCollege = new College("College1", 20, L1, 10);
System.out.println(myCollege);

now your output is:

Name College: College1 num Of Lecturer: 20 all Lecturer: [name: David num Of Times Pen Falls: 3 favorite Ice Cream: Banana auto Number: 1001] max Lecturer 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM