![](/img/trans.png)
[英]How do I initialize a 2D array using the constructor of another class in Java?
[英]How do i initialize array of a another class?
我正在嘗試根據用戶輸入初始化一個數組,以便它可以是任何自定義大小。 默認情況下,我已使用所有 0.0 值將數組初始化為 10,但是當我嘗試調用初始化數組的方法時,沒有任何反應,並且數組中的大小或值均未更改。
班級編號:
public class Numbers {
private Float [] numbers;
private int default_size = 10;
public Numbers() {
/// write code here to intialize a "default" array since this is the default constructor
numbers = new Float[default_size];
for(int i = 0; i < default_size; i++)
numbers [i] = (float) 0.0;
}
public Numbers (int size) {
/// write code here to initialize an "initial" array given the parameter size as this is an initial constructor
numbers = new Float[size];
}
public void initValuesInArray() {
/// write code here to intialize the values in the array
Scanner scan = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.println("Enter Value : ");
numbers[i] = scan.nextFloat();
}
}
public String toString() {
// write code to return the values in the array in a list format - one value per line
for (int i = 0; i < numbers.length; i++)
System.out.println(numbers[i]);
return " ";
}
public float calcAverage() {
// write code to return the average of the values in the array
double sum = 0.0;
for (int i = 0; i < numbers.length; i++)
sum += numbers[i];
double average = sum / numbers.length;
System.out.println(average);
return (float) average;
}
}
班級主要:
public class Main {
public static void main (String [] args) {
// write the code here to implement the menu as specified in Lab 1
boolean menuLoop = true;
while(true) {
System.out.println("Enter 1 to initialize a default array");
System.out.println("Enter 2 to initialize an array of input size");
System.out.println("Enter 3 fill array with values");
System.out.println("Enter 4 display values in array");
System.out.println("Enter 5 to display average of the values in the array");
System.out.println("6 to quit\n");
Numbers obj = new Numbers();
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
switch (i) {
case 1:
obj = new Numbers ();
break;
case 2:
System.out.println("Enter the new size of array: ");
int x = scan.nextInt();
Numbers [] numbers = new Numbers[x];
break;
case 3:
System.out.println("Enter the float numbers as values in the array: ");
obj.initValuesInArray();
break;
case 4:
obj.toString();
break;
case 5:
obj.calcAverage();
break;
case 6:
System.exit(0);
break;
default :
System.out.println("Invalid Entry...");
break;
}
}
}
}
它需要能夠根據用戶輸入更改數組的大小,然后用戶可以將值輸入到數組中。
編輯:
我想我終於在這里找到了這個問題,那就是當我在 switch 語句中使用 break 時,它會將數組初始化為默認值,即 10 和所有 0.0 值。 雖然我不確定如果選擇的選項是 2,如何讓數組保存輸入值
您沒有使用在Numbers
類中創建的public Numbers (int size)
構造函數。 此外,您正在創建一個名為numbers
的新變量,而不是分配本地字段obj
。 這個:
Numbers [] numbers = new Numbers[x];
應該
obj = new Numbers(x);
如果元素數量不固定,則不應使用 Array。 相反,使用List
或ArrayList
(List 的子類)。
您可以在以下位置找到所需的所有方法: developer.android.com/reference/java/util/ArrayList
編輯 24/01/19 :
如果您需要List
或ArrayList
默認為 10 以防萬一用戶沒有輸入值,您可以創建一個IF/ELSE
來自動填充列表,或者使用您已經擁有的SWITCH CASE
例如:
List<Integer> myList = new ArrayList<Integer>();
float defaultValue = 0.0;
case 1:
for(int i = 0; i < 10; i++) {
myList.add(defaultValue)
}
break;
case 2:
System.out.println("Enter the new size of array: ");
int x = scan.nextInt();
for(int i = 0; i < x; i++) {
myList.add(defaultValue)
}
break;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.