我如何初始化另一个类的数组?

时间:2019-01-24 20:22:52

标签: java

我正在尝试根据用户输入初始化数组,以便它可以是任何自定义大小。默认情况下,我已将所有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时,它将数组初始化为默认值,即所有0.0值均为10。虽然我不确定如果选择的选项为2,如何使数组保持输入值

2 个答案:

答案 0 :(得分:1)

您没有使用在public Numbers (int size)类中创建的Numbers构造函数。另外,您正在创建一个名为numbers的新变量,而不是分配本地字段obj。这个:

    Numbers [] numbers = new Numbers[x];

应该是

    obj = new Numbers(x);

答案 1 :(得分:1)

如果元素数量不固定,则不应使用Array。而是使用ListArrayList(List的子类)。

您可以在以下位置找到所需的所有方法: developer.android.com/reference/java/util/ArrayList

EDIT 24/01/19

如果在用户未输入值的情况下默认情况下需要将ListArrayList设置为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;