在Java中从构造函数创建数组对象

在Java中从构造函数创建数组对象,java,arrays,arraylist,constructor,Java,Arrays,Arraylist,Constructor,这是正确的吗?我说的是这个价格部分,还有回报率部分。尝试从构造函数构造和数组对象,并返回集合中的数组对象数。我还在学习,所以请原谅我的术语有点不恰当 public class ArrayFun { // instance variables private ArrayList<ArrayRates> rates; /** * Constructor for objects of class ArrayFun. */ public

这是正确的吗?我说的是这个价格部分,还有回报率部分。尝试从构造函数构造和数组对象,并返回集合中的数组对象数。我还在学习,所以请原谅我的术语有点不恰当

public class ArrayFun
{
    // instance variables
    private ArrayList<ArrayRates> rates;


    /**
     * Constructor for objects of class ArrayFun.
     */
    public ArrayFun()
    {
        this.rates = new ArrayList<ArrayRates>();

    }

    /**
     * Return the number of ArrayRates objects in the collection.
     *
     * @return int          the number of ArrayRates objects in the collection.
     *
     */
    public int getCount()
    {
        return rates.size();
    }
公共类ArrayFun
{
//实例变量
私人ArrayList费率;
/**
*类ArrayFun的对象的构造函数。
*/
公共ArrayFun()
{
this.rates=new ArrayList();
}
/**
*返回集合中ArrayRates对象的数量。
*
*@return int集合中ArrayRates对象的数量。
*
*/
public int getCount()
{
返回率;
}

您的代码没有问题,因为这是指特定对象的当前实例

另一种方法是在声明速率时初始化

public class ArrayFun
{
    // instance variables
    private ArrayList<ArrayRates> rates = new ArrayList<ArrayRates>();

    // constants
    /**
     * Tolerance variable when comparing floating point values in sort method.
     */
    public static final double SORT_DELTA = .001;

    /**
     * Constructor for objects of class ArrayFun.
     */
    public ArrayFun()
    {


    }

    /**
     * Return the number of ArrayRates objects in the collection.
     *
     * @return int          the number of ArrayRates objects in the collection.
     *
     */
    public int getCount()
    {
        return rates.size();
    }
   }
公共类ArrayFun
{
//实例变量
private ArrayList rates=新建ArrayList();
//常数
/**
*在排序方法中比较浮点值时的公差变量。
*/
公共静态最终双排序_DELTA=.001;
/**
*类ArrayFun的对象的构造函数。
*/
公共ArrayFun()
{
}
/**
*返回集合中ArrayRates对象的数量。
*
*@return int集合中ArrayRates对象的数量。
*
*/
public int getCount()
{
返回率;
}
}

您的代码没有问题,事实上,这在所有项目中都很常用。

我看不出上面的代码有什么问题。也就是说,它似乎被切断了,而且您的构造函数只是创建了一个纯空的
ArrayList
。如果您的意图是将要创建的数据传递给
ArrayFun
对象,您尚未成功。这是我的意图。谢谢。它已被切断,我正试图使问题尽可能简短和切中要害,我倾向于长篇大论,并有一些抱怨问题太长。收集的良好做法是提供新的ArrayList(0);