如何将Array返回给Method Java?

时间:2013-12-18 22:21:44

标签: java

//如何将数组返回给Method 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";
           }

    }

3 个答案:

答案 0 :(得分:1)

我不明白你的问题所以我会根据你所写的内容以及你想要做的事来回答一些看似相关的内容。

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

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

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

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

答案 1 :(得分: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();
}

下一个挑战是从更全面的地方加载歌曲列表,如数据库或文件。该任务将特别针对您的数据源。

答案 2 :(得分:0)

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

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

}