我有一个名为“Artist”的类,我可以调用类“Track”的公共方法 getArtists,它在返回值中为我提供另一个类“ArtistSimplified”的数组。我对返回 ArtistSimplified 类字符串的 getName 方法感兴趣。我想知道怎么做。我不能直接调用 ArtistSimplified 类的 getName 方法。
List<Track> tracks = new ArrayList<>();
tracks = Arrays.asList(topTracksRequest.execute());
if(tracks == null) {
throw new ResourceNotFoundException(new ErrorMessage("No songs found for this id:" +artistId));
}
List<Song> songs = new ArrayList<Song>();
for(int i = 0; i < 5; i++) {
Song song = new Song();
song.setTitle(tracks.get(i).getName());
song.setArtist(tracks.get(i).getArtists().getClass().getMethod("getName", ArtistSimplified.class).getReturnType());
}
我被困在这里。song.setArtist(tracks.get(i).getArtists().getClass().getMethod("getName", ArtistSimplified.class).getReturnType());
我尝试了上面的行,但它不起作用。
您的代码正在尝试调用getName()
数组本身。您需要通知Artist
要从中获取名称的索引。
for (int a = 0; a < tracks.get(i).getArtists().length; a++) {
System.out.print(tracks.get(i).getArtists()[a].getName());
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句