0

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.

My 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 < compguess.length; i++){     // start if i < 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 < 3; a++){     // start if a < 3
            computer1.guess();      // start guess method
            for(int n = 0; n < computer1.compguess.length; n++) //print the contents of []
            System.out.println(computer1.compguess[n]);     // print out entire array
        }
        {
            input.close();
        }
    }
}
2
  • 1
    Going off your previous question, you don't need to change the length of the array at all. Since you only want to know if the number between 1 and 10 has been guessed before, have a boolean array of length 10. If the number n is guessed, set the value of the array at index n - 1 to true. If the value in the array is already true, don't guess it again. Commented Oct 17, 2013 at 0:11
  • 1
    When length of your array is unknown use ArrayList Commented Oct 17, 2013 at 0:11

3 Answers 3

2

There is no way to change the length of an array after it is created in Java. Instead, a new larger array must be allocated and the elements must be copied over. Fortunately, there are implementations of the List interface that do this behind-the-scenes for you already, the most common of which is ArrayList.

As the name suggests, an ArrayList wraps an array, offering ways to add/remove elements via methods like add() and remove() (see the afore-linked documentation). If the internal array fills up, a new array that is 1.5x larger is created an the old elements are copied over to it, but this is all hidden from you, which is quite convenient.

Sign up to request clarification or add additional context in comments.

Comments

1

I suggest the use of an arrayList instead. It will adjust size as needed. Create it using ArrayList<Integer> list=new ArrayList<>(); after importing java.util.ArrayList.

You can set values as follows. To set value in position i to the value val, use:

list.set(i, val);

You can add to the end with list.add(someInt); and retrieve with int foo=list.get(position).

"Resizing" an array is possible by only copying it to a larger one. That still generates a new array instead of operating in place. The int to Integer conversions are handled by autoboxing here.

1 Comment

Resizing an array is impossible period. Your footnote does not involve resizing a pre-allocated array, but rather creating a new larger array.
0

You cannot change the length of an array in Java. You will need to create a new one and copy the values over, or use an ArrayList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.