user2884176
user2884176

Reputation: 9

how to change the length of an array

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();
        }
    }
}

Upvotes: 0

Views: 2239

Answers (3)

arshajii
arshajii

Reputation: 129497

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.

Upvotes: 2

nanofarad
nanofarad

Reputation: 41271

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.

Upvotes: 1

Flight Odyssey
Flight Odyssey

Reputation: 2287

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.

Upvotes: 0

Related Questions