title: how to change the length of an array
body: <p>I asked a similar question before, but I couldn't figure out what the problem was. I am new to programming and am confused about how to change the length of an array by setting its initial length to a variable, but it isn't updating.</p>
  
  <p>My code:</p>
  
  <pre><code>import java.util.Scanner;
  
  class computer{
  
      int g = 1;  //create int g = 2
      int[] compguess = new int[g];   //set array compguess = 2
  
      void guess(){
  
          int rand;   //create int rand
          int i;  //create int i
          rand = (int) Math.ceil(Math.random()*10);   // set rand = # 1-10
          for (i = 0; i &lt; compguess.length; i++){     // start if i &lt; the L of the []-1 (1)
  
              if(rand == compguess[i]){   //if rand is equal to the Ith term, break the for loop
                  break;
              }
          }   //end of for loop
          if(i == compguess.length - 1){  //if i is = the length of the [] - 1:
              compguess[g - 1] = rand;    // set the new last term in the [] = rand
              g++;    // add 1 to the length of the [] to make room for another int
              System.out.println(compguess[g - 1]);   // print the last term
          }
      }
  }
  
  public class game1player2 {
  
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          computer computer1 = new computer();    // create new computer object
          for(int a = 0; a &lt; 3; a++){     // start if a &lt; 3
              computer1.guess();      // start guess method
              for(int n = 0; n &lt; computer1.compguess.length; n++) //print the contents of []
              System.out.println(computer1.compguess[n]);     // print out entire array
          }
          {
              input.close();
          }
      }
  }
  </code></pre>