方法的描述

时间:2017-05-14 18:13:22

标签: java algorithm

我有一个方法的描述,但我不明白如何正确使用它。

    /**
 * Add an artist to Karaoke<br>
 * Find the end of the artist arrangement and add the new artist        to that position.<br>
 */

 public void addArtist(String name, String category, String image) {

  for (int i = 0; i < artistas.length; i++) {

    artistas[i] = new Artista(name, category, image);           

  }

}

但我不明白如何完成安排的路线。

我提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我认为您更新的工作解决方案看起来像这样

public void addArtist(String name, String category, String image) {
  artista = Arrays.copyOf(artista, artista.length + 1); 
  for (int i = 0; i < artistas.length; i++) {

   artistas[i] = new Artista(name, category, image);           

  }

}

希望这有帮助!

答案 1 :(得分:0)

如果你只需要在最后找到第一个空索引,那么检查null就足够了,因为对象数组是用null初始化的。

public void addArtist(String name, String category, String image) {
    for (int i = 0; i < artistas.length; i++) {
        if(artistas[i] == null) {
            artistas[i] = new Artista(name, category, image);
            break;
        }           
    }
}

注意:如果数组中两个null对象之间的某处有Artista值,则上述代码将不再起作用。在这种情况下,新的Artista将被插入到该索引中。