简体   繁体   中英

I'm trying to understand dynamic arrays, I'm stuck on line 7, this(16); Is reference to the size of the array?

I'm starting at Free Code Camps but I had never seen line 7-- this(16); I would appreciate if you told me what it does and where I can read more about it.

public class DynamicArray<T> implements Iterable<T> {

  private T[] arr;// define atributo array del tipo t(vector)
  private int len = 0; // length user thinks array is
  private int capacity = 0; // Actual array size

  public DynamicArray() {
      this(16);   // <-- This line
  }

  public DynamicArray(int capacity) {
    if (capacity < 0) throw new IllegalArgumentException("Illegal Capacity: " + capacity);
    this.capacity = capacity;
    arr = (T[]) new Object[capacity];
  }

  

"this" means the object itself, which is an instance of class DynamicArray. You can get an instance of DynamicArray by calling

DynamicArray myArray = new DynamicArray();

Then myArray is the object that this points to. While this(16) is calling public DynamicArray(int capacity) on the object myArray . So,16 is the argument that will be passed to the parameter capacity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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