简体   繁体   English

Java中的对象arraylist初始化

[英]Object arraylist initialization in Java

I have an object called Carriage . 我有一个名为Carriage的对象。 When I initialize it I have to send a number of seats to the constructor: 当我初始化它时,我必须发送一些座位给构造函数:

Carriage newCarriage = new Carriage(30); //30=num of seats

Now I created a new class called Train , and this is its constructor: 现在,我创建了一个名为Train的新类,这是它的构造函数:

public class Train {
    ArrayList<Carriage> trainCarriageList;

    Train(int baseTrainSize,int baseCarriageSize){
        trainCarriageList= new ArrayList<Carriage>(baseTrainSize);
    }
}

Now the question is: How do I initiate a train arraylist with the size of "baseTrainSize" and each Carriage with the size of baseCarriageSize correctly? 现在的问题是:如何正确初始化大小为“ baseTrainSize”的火车数组列表以及每个大小为baseCarriageSize的Carriage

When you pass the size (x) to the constructor of an ArrayList you DON'T initialize it with x new elements, you initialize it with exactly 0 elements. 当您将大小(x)传递给ArrayList的构造函数时, 请勿使用x个新元素对其进行初始化,而应使用完全0个元素对其进行初始化。 The capacity is x. 容量为x。 The inner implementation of ArrayList is a regular array which will start with size x . ArrayList的内部实现是一个常规数组,其大小为x When the array is filled, the array list initializes a new array of size 2x and copies the array of size x to it. 填充数组后,数组列表将初始化一个2x大小的新数组,并将x大小的数组复制到其中。 This is a costly step. 这是一个昂贵的步骤。 By declaring the size, you can spare this step if you know exactly how many elements will be in the ArrayList . 通过声明大小,如果您确切知道ArrayList中将包含多少个元素,则ArrayList此步骤。

You can use a loop for that. 您可以为此使用循环。 But logically speaking, just looping to add elements all initialized with the same value does not make sense to me. 但是从逻辑上讲,仅循环添加所有用相同值初始化的元素对我来说是没有意义的。 Rather add one more constructor to have the default value , whatever you need. 而是根据需要添加一个构造函数以使用默认值。 Although if you really need to do that then do something like this: 尽管如果您确实需要这样做,请执行以下操作:

for(int i=0 ; i < baseTrainSize; i++) {
Carriage carriage = new Carriage(30);
trainCarriageList.add(carriage);
}

An ArrayList<Carriage> does not contain any Carriage until you don't add them to the collection. ArrayList<Carriage>在您不将其添加到集合之前不包含任何Carriage

This means that you shouldn't care about initialising how many seats a Carriage has when you initialise the container for them but just when you initialise them. 这意味着在为容器初始化容器时,您不必在意初始化托架有多少个座位。

trainCarriageList = new ArrayList<Carriage>(baseTrainSize);
...
Carriage carriage = new Carriage(30);
trainCarriageList.add(carriage);

You could have Class Train implement List. 您可以使用Class Train工具清单。 This way, your train object would literally be a list of Carriage s. 这样,您的火车对象实际上就是Carriage的列表。

Use this: 用这个:

public class Train {
  ArrayList<Carriage> trainCarriageList;

  Train(int baseTrainSize,int baseCarriageSize){
    // initialize the List with the base size:
    trainCarriageList= new ArrayList<Carriage>(baseTrainSize);
    // initialize each list element with it's own base size:
    for(int i=0;i<baseTrainSize;i++){
      trainCarriageList.set(i, new Carriage(baseCarriageSize));
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM