java - 直接在数组中添加对象

标签 java arrays arraylist

我已经为学校作业编写了一个对象。现在我正在尝试将其添加到数组中,以便我可以添加多个框。

我的主要内容

String name;
        double userInputLength;
        double userInputWidth;
        double userInputHeight;

        // initialise a scanner to be able to read the user input
        Scanner reader = new Scanner(System.in);

        // ask the user for input
        System.out.print ("Enter the name of your box: ");
        // read this input
        name = (reader.nextLine());

        // ask the user for input
        System.out.print ("Enter the length of your box: ");
        // read this input
        userInputLength = (reader.nextDouble());

        // ask the user for input
        System.out.print ("Enter the width of your box: ");
        // read this input
        userInputWidth = (reader.nextDouble());

        // ask the user for input
        System.out.print ("Enter the height of your box: ");
        // read this input
        userInputHeight = (reader.nextDouble());

        Block blockOne = new Block(name, userInputLength, userInputWidth, userInputHeight);
        System.out.println( blockOne.showBoxAsString());

我的对象

package model;

import java.util.ArrayList;
import java.util.List;

public class Block {

    // name variable of the figure
    private String name;

    // dimension variable of the figure
    private double blockWidth;
    private double blockHeight;
    private double blockLength;

    public Block() {

    }

    // form a block
    public Block(String N, double L, double W, double H){
        this.name = N;
        this.blockLength = L;
        this.blockWidth = W;
        this.blockHeight = H;
    }

    // set the name
    public void setBlockName(String N){ this.name = N; }
    // set the name
    public String getBlockName(){ return this.name; }

    //set length method
    public void setLength(double L)
    {
        this.blockLength = L;
    }
    //get length method
    public double getLenght(){
        return this.blockLength;
    }


    //set width method
    public void setWidth(double W)
    {
        this.blockLength = W;
    }
    //get width method
    public double getWidth(){
        return this.blockWidth;
    }


    //set height method
    public void setHeight(double H)
    {
        this.blockLength = H;
    }
    //get height method
    public double getHeight(){
        return this.blockHeight;
    }


    // method to calculate the surface of the shape (in this situation its a box)
    public double calcSurfaceBox(){
        // the formula to calculate the surface of the box is length times the width
        double surface = 2 * (this.blockHeight * this.blockWidth) +
                         2 * (this.blockLength * this.blockHeight) +
                         2 * (this.blockWidth * this.blockLength) ;

        // return the calculated value of surface
        return surface;
    }

    // method to calculate the volume of the shape (in this situation it's a box)
    public double calcVolumeBox(){
        // the formula to calculate the volume is length times width times height
        double volume = this.blockLength * this.blockWidth * this.blockHeight;

        // return the calculated value of volume
        return volume;
    }

    // a method to print a string to show the user the size of the shape (in this case a box.)
    public String showBoxAsString(){
        return String.format( "The box has a name: " + getBlockName() + "\n" +
                              "The box has a Length of: " + getLenght() + "\n" +
                              "The box has a Width of: " + getWidth() + "\n" +
                              "The height of the box is: " + getHeight());
    }

}

我搜索了多种解决方案,但我无法弄清楚。有谁可以给​​我一些工具或想法吗?

我的目标是使我的主要代码尽可能干净。因此,如果有人知道我如何简化我的主要代码,那就太棒了。

最佳答案

初始化 block 对象数组
block blocks[] = new Block[length];
要访问每个对象,请使用blocks[index]
注意:这里的数组元素存储的是对象的引用变量
编辑:这里的索引意味着一个 block 对象,也许是blockOne

关于java - 直接在数组中添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440508/

相关文章:

java - 为什么正在读取的文件中的行没有添加到我的 ArrayList 中?

java - JAX-WS : Return String as CDATA

java - 导入模块中的 Spring Boot 和 Controller

c - 从函数内部使用函数外部定义的数组

sql - Codeigniter Active Record - 删除不在数组中的位置

javascript - Ruby on Rails - 将地理编码器对象作为谷歌地图标记数组

java - 无法从java连接到hbase

java关于ResultSet的问题

java - hashmap 和 arraylist 连接

java - 新的 ArrayList<int>() 在 Java 中失败