簡體   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