简体   繁体   中英

“cannot be converted to String[] by method invocation conversion”

I am sure this is something stupid, but I can't figure it out for the life of me.... In the main method, when I am trying to create new artists, I keep getting an error on the creating a new "Recording" line (ie: surfacing and pop). It is saying it requires String, String[] but is getting String, MusicCollection.Artist. And it says "actual argument MusicCollection.Artist cannot be converted to String[] by method invocation conversion.

public class MusicCollection {

    private Artist[] artists = new Artist[100];
    private Recording[] recordings = new Recording[200];
    private int artistCount = 0;
    private int recordingCount = 0;

//toString method for MusicCollection
    public String toString() {

        StringBuffer sb = new StringBuffer();
        if (recordingCount > 0) {
            sb.append(recordings[0].toString());
            for (int i = 1; i < recordingCount; i++) {
                sb.append("\n" + recordings[i]);
            }
        }
        return sb.toString();
    }

    public class Artist {

        private String name;

        /**
         * Construct an artist object and add it to the collection.
         *
         * @param name the name of the Artist
         */
        public Artist(String name) {
            this.name = name;
            artists[artistCount++] = this;
        }

        /**
         * Retrieve the artist as a string
         *
         * @return the string representation of the artist
         */
        public String toString() {
            return name;
        }
    }

    public class Recording {

        private String name;
        private Artist[] artists = new Artist[100];
        private Track[] tracks = new Track[200];
        private int trackCount = 0;

        public class Track {

            private String name;

            /**
             * Construct track object and add it to the collection.
             *
             * @param name the name of the track
             */
            public Track(String name) {
                this.name = name;
                tracks[trackCount++] = this;
            }

            /**
             * Retrieve the track as a string
             *
             * @return the string representation of the track
             */
            public String toString() {
                return name;
            }
        }

        public Recording(String name, String Artist[]) {

            this.name = name;
            this.artists = artists;

            recordings[recordingCount++] = this;
        }

        public String toString() {
            StringBuffer sb = new StringBuffer(name);
            sb.append(" by " + artists + ": ");
            if (trackCount > 0) {
                sb.append(tracks[0].toString());
                for (int i = 1; i < trackCount; i++) {
                    sb.append(", " + tracks[i]);
                }
            }
            return sb.toString();
        }
    }

    public static void main(String[] args) {
        MusicCollection mc = new MusicCollection();
        Artist sarahM = mc.new Artist("Sarah McLachlan");
        Recording surfacing = mc.new Recording("Surfacing", sarahM);
        Recording.Track surfacing1 = surfacing.new Track("Building a Mystery");
        Recording.Track surfacing4 = surfacing.new Track("Adia");

        Artist u2 = mc.new Artist("U2");
        Recording pop = mc.new Recording("Pop", u2);
        Recording.Track pop1 = pop.new Track("Discotheque");
        Recording.Track pop5 = pop.new Track("Miami");
        System.out.println(mc);
    }
}

Needed to have: public Recording(String name, Artist someArtist) and in my Recording class, only have:

private Artist artist;

since I had already declared Artist as an array. I also had to change this.artists = artists; to this.artists = someArtist; , since that was the variable I am passing. Worked like a charm after!

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