繁体   English   中英

“无法通过方法调用转换将其转换为String []”

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

我敢肯定这是愚蠢的,但是我无法终生解决。...在主要方法中,当我尝试创建新的艺术家时,在创建新的“录制”行(即:铺面和弹出)。 据说它需要String,String []但正在获取String,MusicCollection.Artist。 它说:“实际参数MusicCollection.Artist无法通过方法调用转换转换为String []。

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);
    }
}

需要具有: public Recording(String name, Artist someArtist) ,在我的Recording类中,仅具有:

private Artist artist;

因为我已经声明Artist为数组。 我还必须更改此this.artists = artists; to this.artists = someArtist; ,因为那是我要传递的变量。 后像魅力一样工作!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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