在Java中存储数组中的对象

在Java中存储数组中的对象,java,arrays,Java,Arrays,这里有一个简单的程序。我被指定将对象存储在数组中。但由于我是一名初学者,所以我不知道如何在数组中存储对象。有人能帮我回答这个问题吗 import java.util.Scanner; public class MainExample { public static void main(String[] args) { double length; double width; double price_per_sqyd; doubl

这里有一个简单的程序。我被指定将对象存储在数组中。但由于我是一名初学者,所以我不知道如何在数组中存储对象。有人能帮我回答这个问题吗

    import java.util.Scanner;

    public class MainExample {

    public static void main(String[] args) {

    double length; 
    double width; 
    double price_per_sqyd;
    double price_for_padding; 
    double price_for_installation;
        String input; 
    double final_price; 
        boolean repeat = true;


    Scanner keyboard = new Scanner(System.in);

        while (repeat)
        {   


    System.out.println("\n" +"What is the length of the room?: ");
    length = keyboard.nextInt();

    System.out.println("What is the width of the room?: ");
    width = keyboard.nextInt();

    System.out.println("What is the price of the carpet per square yard?: ");
    price_per_sqyd = keyboard.nextDouble();

        System.out.println("What is the price for the padding?: ");
        price_for_padding = keyboard.nextDouble();

        System.out.println("What is the price of the installation?: ");
        price_for_installation = keyboard.nextDouble();


        keyboard.nextLine();

        System.out.println( "\n" + "Type 'yes' or 'no' if this is correct: ");
        input = keyboard.nextLine();

        if ("yes".equals(input)) 
         repeat = true; 
        else 
         repeat = false; 

        } 
    }
}

虽然不是很优雅,但这个玩具程序的一个快速解决方案是创建一个具有各种属性(如长度、宽度、每平方英尺价格等)的Room类。然后您可以设置每个Room对象的特定属性,并将Room对象存储在Room数组中。

您想在数组中存储什么?不同的最终价格值

无论如何,因为您不知道循环将运行多少次,所以可能需要一个ArrayList。您可以通过添加ArrayList prices=new ArrayList

然后,在循环末尾添加一行,将正确的变量存储到ArrayList中。例如:prices.addfinal_price

如果最终价格不是您想要存储的,那么只需将其替换为您想要存储的变量即可


另外,不要忘记,如果使用ArrayList,则需要在代码顶部使用正确的import语句:import java.util.ArrayList

您需要创建一个类来保存这样的属性。创建一个构造函数来初始化这些值

public class Room{
     double length; 
   double width; 
    double price_per_sqyd;
    double price_for_padding; 
    double price_for_installation;
    String input; 
    double final_price; 
    boolean repeat = true;

}
然后在主方法/驱动程序类中创建一个此类类型的数组并存储相关对象

Room arr=new Room[100];
 int count=0;
     while (repeat)
        {   


    System.out.println("\n" +"What is the length of the room?: ");
    length = keyboard.nextInt();

    System.out.println("What is the width of the room?: ");
    width = keyboard.nextInt();

    System.out.println("What is the price of the carpet per square yard?:      ");
    price_per_sqyd = keyboard.nextDouble();

        System.out.println("What is the price for the padding?: ");
        price_for_padding = keyboard.nextDouble();

    System.out.println("What is the price of the installation?: ");
    price_for_installation = keyboard.nextDouble();


        keyboard.nextLine();

        System.out.println( "\n" + "Type 'yes' or 'no' if this is correct: ");
    input = keyboard.nextLine();
arr[count]=new Room(length,width,price1,price2,price3,price4);//call to the constructor
    if ("yes".equals(input)) 
     repeat = true; 
count++;
    else 
     repeat = false; 

    } 
}

你的问题不清楚。您试图存储什么?使用Object[]objArray=new Object[length]如果您的代码没有对象数组,那么您想在对象数组中存储什么。非常感谢您的帮助。@MohammedSohailEbrahim-对不起,现在我有另一个问题。既然对象存储在数组中,我想从数组中打印对象。例如,用户输入yes并输入许多房间的大小,但当用户输入no时,它将停止。程序停止后,我想重新打印用户在一行中输入的所有内容。我希望我清楚地描述了这个问题。嗯,您可能希望在对象类中包含一个toString方法。这种方法应该协调对象的所有数据字段,并以您选择的任何形式返回它。之后,只需循环遍历数组并打印数组[whatever index];让我知道这是怎么回事抱歉我离开了一段时间我做了一些更改,现在它打印了对象,但仍然会出错。请你检查一下:System.out.println谢谢!;对于int j=0;j<房间长度-1;j++{System.out.printlnLength=+房间[j]。getLength;System.out.printlnwidth=+房间[j]。getWidth;等等