Java &引用;无法通过方法调用转换转换为字符串[]”;
我肯定这是件愚蠢的事,但我一辈子也弄不明白。。。。在主要方法中,当我尝试创建新的艺术家时,我在创建新的“录制”行(即:Surface和pop)时不断遇到错误。它说它需要String,String[],但得到的是String,MusicCollection.Artist。它说“实际参数MusicCollection.Artist不能通过方法调用转换转换为String[]Java &引用;无法通过方法调用转换转换为字符串[]”;,java,Java,我肯定这是件愚蠢的事,但我一辈子也弄不明白。。。。在主要方法中,当我尝试创建新的艺术家时,我在创建新的“录制”行(即:Surface和pop)时不断遇到错误。它说它需要String,String[],但得到的是String,MusicCollection.Artist。它说“实际参数MusicCollection.Artist不能通过方法调用转换转换为String[] public class MusicCollection { private Artist[] artists = n
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);
}
}
公共类音乐收藏{
私人艺术家[]艺术家=新艺术家[100];
私人录音[]录音=新录音[200];
私人int artistCount=0;
私有int-recordingCount=0;
//音乐采集的toString方法
公共字符串toString(){
StringBuffer sb=新的StringBuffer();
如果(记录计数>0){
sb.append(录音[0].toString());
对于(int i=1;i0){
sb.append(曲目[0].toString());
对于(int i=1;i
需要具备:
公开录制(字符串名称,艺术家)
在我的录音课上,只有:
private Artist artist;
因为我已经宣布艺术家为数组。
我还必须将
this.artists=artists;
更改为this.artists=someArtist;
,因为这是我要传递的变量。在公开录制之后工作得很有魅力!(字符串名称,字符串艺术家[]){
您想要一个String
数组还是Artist
s数组?因此,将变量声明为Artist
类型。当前它声明为类型String
,名称为Artist
。类似于公共录制(String name,Artist someArtist[]){}
应该可以工作。实际上,这一行:Recording surfacing=mc.new Recording(“surfacing”,sarahM);
告诉我您希望您的构造函数类似于:public Recording(字符串名称,艺术家someArtist){
因为您传递的是一个Artist
对象,而不是它的数组。在打印输出中获取意外的十六进制值通常意味着您忘记在类上实现.toString(),而正在获取默认对象.toString()实现。