Java 如何初始化另一个类的数组?
我正在尝试根据用户输入初始化一个数组,以便它可以是任何自定义大小。默认情况下,我已使用所有0.0值将数组初始化为10,但当我尝试调用初始化数组的方法时,什么都不会发生,并且数组中的值和大小都不会改变 课程编号:Java 如何初始化另一个类的数组?,java,Java,我正在尝试根据用户输入初始化一个数组,以便它可以是任何自定义大小。默认情况下,我已使用所有0.0值将数组初始化为10,但当我尝试调用初始化数组的方法时,什么都不会发生,并且数组中的值和大小都不会改变 课程编号: public class Numbers { private Float [] numbers; private int default_size = 10; public Numbers() { /// write code here to intialize a "def
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;
}
}
它需要能够根据用户输入更改数组的大小,然后用户可以在数组中输入值
编辑:
我想我终于解决了这个问题,当我在switch语句中使用break时,它将数组初始化为默认值,即所有0.0值都为10。虽然我不知道如果选择的选项为2,如何使数组保持输入值,但您没有使用在
数字类中创建的公共数字(int size)
构造函数。另外,您正在创建一个名为numbers
的新变量,而不是分配本地字段obj
。这:
Numbers [] numbers = new Numbers[x];
应该是
obj = new Numbers(x);
如果元素数量不固定,则不应使用数组。相反,使用List
或ArrayList
(List的子类)
您可以在以下位置找到所需的所有方法:
19年1月24日编辑:
如果您需要在默认情况下将列表
或数组列表
设置为10,以防用户未输入值,则可以创建一个If/ELSE
自动填充列表,或者使用您已有的切换框
例如:
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;
List myList=new ArrayList();
浮动默认值=0.0;
案例1:
对于(int i=0;i<10;i++){
myList.add(默认值)
}
打破
案例2:
System.out.println(“输入数组的新大小:”);
int x=scan.nextInt();
对于(int i=0;i
您正在做的是Numbers[]Numbers=new Numbers[x]
而不是obj=new Numbers[x]
。我已经试过了,但是当我这么做时,它会给我一个类型不匹配错误。为什么不使用ArrayList来避免数组大小的问题呢,我建议为size指定一个默认值,并在实现中进行检查。使用List
或ArrayList
实际上没有意义。你的意思是使用LinkedList
或ArrayList
吗?如果用户不想输入数组大小,我需要将其默认为10,因此默认情况下必须将其设置为10。我刚刚添加了一个解决方案。希望它能工作。这段代码不会工作,因为您不能将原语用作类型<代码>ArrayList myList=新建ArrayList()代码>此外,如果可能,您应该针对接口编写代码,因此应该是:List myList=new ArrayList()
这在技术上是可行的,但我不能在这个解决方案中使用ArrayList,不幸的是,它仍然对数组大小没有任何影响。数组的值或大小不匹配change@JustinMacwan我再次阅读了你的文章并编辑了我的答案。不幸的是,它仍然没有改变数组的大小,并且在0.0下保持为10values@JustinMacwan你能告诉我你是怎么知道的吗?在案例2中设置数组大小后,请在读取数组长度的位置共享代码。
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;