繁体   English   中英

如何将数组返回给方法Java?

[英]How do I return an Array to a Method Java?

//如何将数组返回给方法Java?

    public class Song2 {
          String name;
          String artist;
          String genre;
          String year;

          public void intro(){
            System.out.println("Welcome to the song!");
          }

          public void verse(){
            System.out.println("Let's take you to a verse");
          }

           public void chorus()
            System.out.println("And now how about a chorus?");
          }

           public void end(){
            System.out.println("Here's the end of the song");
           }

    }

//this is my array.
    import javax.swing.JOptionPane;
    public class SongTestDrive2
    {
        public static void main(String[] args)
        {

            Song2[] mySong2 = new Song2[2];
            int x = 0;
            mySong2[0] = new Song2();
            mySong2[1] = new Song2();
            mySong2[0].name = "Song Title: soul to squeeze";
            mySong2[1].name = "Song Title: Slaughtered";
            mySong2[0].artist = "Artist: Red Hot Chili Peppers";
            mySong2[1].artist = "Artist: PanterA";
            mySong2[0].genre = "Genre: Funk Rock";
            mySong2[1].genre = "Genre: Groove Metal";
            mySong2[0].year = "Year: 1993";
            mySong2[1].year = "Year: 1994";
           }

    }

我不明白您的问题,因此我只会根据您所写的内容和您想做的事情回答似乎相关的问题。

private String[] myMethod() {
   String[] myArr = {"1", "2", "3"};
   return myArr;
}

这就是方法返回数组的方式。

要将数组作为参数传递给方法,您只需指定该方法将数组作为参数即可

private void myMethod(String[] data) {
   System.out.println(data[0]);
}

这只是一个返回类型:

public Song[] getSongs() {
   // populate an array of songs. 
   // This is a basic implementation that loads data statically.
   Song2[] mySong2 = new Song2[2];
   mySong2[0] = new Song2();
   mySong2[1] = new Song2();
   mySong2[0].name = "Song Title: soul to squeeze";
   mySong2[1].name = "Song Title: Slaughtered";
   mySong2[0].artist = "Artist: Red Hot Chili Peppers";
   mySong2[1].artist = "Artist: PanterA";
   mySong2[0].genre = "Genre: Funk Rock";
   mySong2[1].genre = "Genre: Groove Metal";
   mySong2[0].year = "Year: 1993";
   mySong2[1].year = "Year: 1994";
}

假设这在您的SongTestDrive2类的主目录中:

public static void main(String[] args) {
    Song [] songs = getSongs();
}

下一个挑战是从更全面的位置(例如数据库或文件)加载歌曲列表。 该任务将非常特定于您的数据源。

在这里,您可以找到一个完全合理且可运行的完整代码。

package try1;
import try1.Song;

public class Main {

public static void main(String[] args){
    //creates a Song Array and calls the addTwoSongs method to fill the mySongs array
    Song[] mySongs = addTwoSongs(); 
    //prints some stuff of the existing objects in the array
    mySongs[0].printintro();
    mySongs[0].printchorus();
    mySongs[1].printverse();
    mySongs[1].printend();
}

public static Song[] addTwoSongs(){
    //creates new Song Array to save two Songs
    Song[] tmpSongs = new Song[2];
    //creates two new Songs and adds to the array
    tmpSongs[0] = new Song("soul to squeeze", "Red Hot Chili Peppers", "Funk Rock", "1993");
    tmpSongs[1] = new Song("Slaughtered", "PanterA", "Groove Metal", "1994");
    //returns the array
    return tmpSongs;
}
}

两个类,一个主对象和对象Song

package try1;

public class Song {
String name;
String artist;
String genre;
String year;

//Constructor to set all attributes with init of object
public Song(String tmpName, String tmpArtist, String tmpGenre, String tmpYear){
    this.name=tmpName;
    this.genre=tmpGenre;
    this.artist=tmpArtist;
    this.year=tmpYear;
}
//empty Constructor
public Song(){

}

public void printintro(){
  System.out.println("Welcome to the song!");
}

public void printverse(){
  System.out.println("Let's take you to a verse");
}

public void printchorus(){
  System.out.println("And now how about a chorus?");
}

public void printend(){
  System.out.println("Here's the end of the song");
}

}

暂无
暂无

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

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