我在使用用户输入动态创建新对象时出现问题。我知道如何使用ArrayList,但我想知道是否可以只使用一个数组? Object 1
和Object 2
从MainObject
延伸。
我目前有:
import java.util.Scanner;
public class Main
{
public static void main (String args[])
{
MainObject[] Main = new MainObject[99];
//^objects created will be added to this array^
int input;
Scanner scanner = new Scanner(System.in);
do
{
System.out.println("1. Add a new object 1");
System.out.println("2. Add a new object 2");
System.out.println("3. Display all object info");
System.out.println("4. Quit");
System.out.print("Please enter either 1 to 4: ");
input =(scanner.nextLine());
switch(input) {
case 1 :
object1 obj1 = new object1();
System.out.println("Please enter name of object: ");
obj1.setName(scanner.nextLine());
obj1.display();
case 2 :
object2 obj2 = new object2();
System.out.println("Please enter name of object: ");
obj2.setName(scanner.nextLine());
obj2.display();
case 3 :
//this is where the for loop should be to display all the info of obj 1 and 2
case 4 :
System.out.println("Thank You");
break;
}
}
while (input==1 || input==2 || input==3)
所以我把对象添加到数组中就像这样
case 1 :
object1 obj1 = new object1();
System.out.println("Please enter name of object: ");
obj1.setName(scanner.nextLine());
obj1.display();
Main[0] = obj1;
break;
case 2 :
object2 obj2 = new object2();
System.out.println("Please enter name of object: ");
obj2.setName(scanner.nextLine());
obj2.display();
Main[1] = obj2;
break;
case 3 :
int x = 0;
for (x=0; x<Main.length; x++)
{
Main[x].displayComputer();
}
break;
编译并运行它工作正常但它给了我一个java.lang.NULLPointerException:null和导致问题的突出显示的代码是
Main[x].displayComputer();
答案 0 :(得分:5)
ArrayList
可以具有可变大小,而数组具有静态大小。也就是说,一旦分配了数组,就无法追加/插入新元素。但是,您可以分配一个大型数组,然后逐个填充它。通常,该过程如下所示:
int nextSpot = 0; //next spot to fill in array
while (still_getting_input) {
if (supposed_to_insert) {
if (nextSpot_in_valid_range)
myArray[nextSpot++] = value_to_insert;
else
System.out.println("Invalid operation!"); //cannot insert.
}
}
所以,你的程序看起来像是:
import java.util.Scanner;
public class Main
{
public static void main (String args[])
{
MainObject[] Main = new MainObject[99];
//^objects created will be added to this array^
String input;
Scanner scanner = new Scanner(System.in);
int nextSpot = 0;
do
{
System.out.println("1. Add a new object 1");
System.out.println("2. Add a new object 2");
System.out.println("3. Display all object info");
System.out.println("4. Quit");
System.out.print("Please enter either 1 to 4: ");
input =(scanner.nextLine());
switch(input) {
case 1 :
if (nextSpot < Main.length) {
object1 obj1 = new object1();
System.out.println("Please enter name of object: ");
obj1.setName(scanner.nextLine());
obj1.display();
Main[nextSpot++] = obj1;
}
else {
System.out.println("Error!");
}
break;
// etc.
}
}
while (input==1 || input==2 || input==3)
您的代码还存在其他一些问题(特别是您使用switch
语句;您将体验到堕落),但这会回答您提出的问题。
答案 1 :(得分:1)
我会做这样的事情,
int index = 0;
do {
...
case 1 :
...
Main[index++] = obj1;
break;
case 2 :
...
Main[index++] = obj2;
break;
case 3:
// Iterate over the loop till index
} while ((index < Main.length && (input==1 || input==2)) || input==3)
答案 2 :(得分:1)
你的问题实际上是迭代时的数组和arraylist,它们需要多长时间?
当您遍历数组时,它的正常迭代将达到您在初始化时为数组声明的大小。但是对于arrayList,它是在arrayList中实际存在多少个元素。在内部,arrayList在默认容量之后需要时将其大小加倍。
您可以跟踪要添加的元素数量。 或者只对索引元素执行 NULL POINTER CHECK
答案 3 :(得分:0)
请注意,您错过了将值加载到Main[]
数组中,因为只初始化了对象obj1
(以及类似的obj2 ...)。维护索引并相应地将对象添加到数组中:
int index=0;
...
case 1 :
object1 obj1 = new object1();
System.out.println("Please enter name of object: ");
obj1.setName(scanner.nextLine());
obj1.display();
Main[index++] = obj1;
...
while((index < 100 && (input==1 || input==2 || input==3))