title: Retrieving length of an array in a method doesn't work
body: <p>I'm fairly new to Java so please imagine that I'm 5 years old and require an answer suitable for a five year old.</p>
  
  <p><strong>First some information:</strong>
  I'm trying to make a "Dynamic array" that grows and shrinks when needed. I know of arraylists, the purpose of this is that i need to be able to do this myself and also learn at the same time since i assume this is how <code>ArrayList</code>s work anyway.</p>
  
  <p><strong>The problem:</strong> I want to add some fancy methods for the reason above one of which is to calculate the mean value of the whole array.
  when i try to get the length (<code>varible.length</code>) so does it not work while in some other methods it does!</p>
  
  <p>This is the method that doesn't work:</p>
  
  <pre><code>public double meanValue() {
      double meanDiv = 0;
      int div = x.length;
  
      for (int i = 0 ; i &lt; div ; i++) {
          meanDiv += x[i];
          System.out.println("Testing");
      }
      meanDiv = meanDiv/x.length;
      return meanDiv;
  }   
  </code></pre>
  
  <p>It's returning NaN (not a number), but if you skip the division part it returns 0, it's totally not reading <code>x.length</code>.</p>
  
  <p>Worst part? Some methods are working, this one for example to return the last position:</p>
  
  <pre><code>public long last() {
      long pepe = -1;
      if (x.length==0) {
          System.out.println("Nothing in the array! Returning -1");
      }
      else {
          pepe = x[x.length-1];
      }
      return pepe;
  }    
  </code></pre>
  
  <p>I've tried way too many things, different kind of variables, while instead of for, etc.</p>
  
  <p>Here is the whole class if it's required:</p>
  
  <pre><code>public class dynamicArray {
      private long[] x;
      private int size = 0;
  
      public dynamicArray() {
          x = new long[size];
      }                                         // CREATES THE ARRAY  
  
      public long grab(int x) {
          if (x &lt; this.x.length) {
              return this.x[x];
          }
          else {
              System.out.println("(GET) Out of bounds");
              return -1;
          }
      }
      public void add(long value) {
          dynamicInAction();
          x[size] = value;
          size++;
      }                                   // ADD A NUMBER TO THE ARRAY
      public void remove(int position) {
  
      }                              // REMOVES A SPECIFIC ARRAY POSITION----------------
      public long last() {
          long pepe = -1;
          if (x.length==0) {
              System.out.println("Nothing in the array! Returning -1");
          }
          else {
              pepe = x[x.length-1];
          }
          return pepe;
      }                                           // RETURNS THE VALUE IN THE LAST POSITION
      public long first() {
          long pepe = -1;
          if (x.length==0) {
              System.out.println("Nothing in the array! Returning -1");
          }
          else {
               pepe = x[0];
          }
          return pepe;
      }                                          // RETURNS THE VALUE IN THE FIRST POSITION
      public void modify(int position, int value) {
          if (position &lt; x.length &amp;&amp; position &gt;= 0) {
              x[position] = value;
          }
          else {
              System.out.println("This position does not exist");
          }
      }                   // MODIFIES A SPECIFIC POSITION TO THE ASSIGNED VALUE
      public int size() {
          return x.length;
      }                                             // RETURNS THE CURRENT ARRAY SIZE
      public double meanValue() {
          double meanDiv = 0;
          int div = x.length;
  
          for (int i = 0 ; i &lt; div ; i++) {
              meanDiv += x[i];
              System.out.println("Testing");
          }
          meanDiv = meanDiv/x.length;
          return meanDiv;
      }                                       // RETURNS THE MEAN VALUE OF THE WHOLE ARRAY-------------
      public long median() {
          return -1;
      }                                          // RETURNS THE MEDIAN VALUE OF THE WHOLE ARRAY-------------
      private void dynamicInAction() {
          if (x.length == size) {
              long[] temp = new long[x.length];
              for (int i = 0; i&lt;x.length ; i++) {
                  temp[i] = x[i];
              }
              x = null;
              //System.gc();   // Might be unnecessary
              x = new long[size+1];
              for (int i = 0; i&lt;temp.length ; i++) {
                  x[i] = temp[i];
              }
          }
      }                               // ENSURES THE ARRAY GROWS WHEN NEEDED
      public void reverse() {
  
      }                                         // REVERSING ALL POSITIONS IN THE ARRAY------------------
      public void clear() {                                                                                                          
         x = null;
         size = 0;
         x = new long[size];
      }                                           // CLEARS THE ARRAY
  
  
  
  }
  </code></pre>