[英]Java Arrays - How to create an array using a static method
我正在搜索使用名為insertRow(int [] row)的方法創建新數組的代碼行。 使用此方法,用戶可以插入5個數字以形成數組。 然后這個數組應該命名為row2。 請幫忙。
public class App
{
public static void main(String[] args)
{
int[] row = new int[5];
int[] row1 = {2,7,1,9,4};
//int[] row2 = insertRow(row); this is wrong
}
public static void insertRow(int[] row)
{
for (int i = 0; i < row.length; i++)
{
int number;
do
number = Integer.parseInt(JOptionPane.showInputDialog("Insert the " + (i+1) + "th positif number"));
while (getal < 0);
row[i] = number;
}
}
}
你是在正確的軌道上:更改方法的簽名以返回int[]
,在內部分配row
,並將代碼放在...
下面的位置:
public static int[] insertRow() {
int[] row = new int[5];
...
return row;
}
現在這將工作:
int[] row2 = insertRow(); // this is no longer wrong :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.