gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:41:57 25 4
gpt4 key购买 nike

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

关于java - 如何将数组返回给 Java 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20669679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com