简体   繁体   中英

How do I return an Array to a Method Java?

//How do I return an Array to a 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";
           }

    }

I don't understand what your question so I will just answer something that seems relevant based on what you've written and what you seem to want to do.

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

This is how a method returns an array.

To pass an array to a method as an argument you just specify that the method takes an array as an argument

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

It's just a return type:

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

Assuming this is in your SongTestDrive2 class, in your main:

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

The next challenge is to load the songs list from somewhere more comprehensive like a database or a file. That task will be very specific to your data source.

Here you go, a perfectly reasonable and run able complete code.

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

Two classes, one main and the object 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");
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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