1

When I learned java, I was told that once created, the size of an array was fixed and couldn't be changed. Recently I've been using arrays a lot and have noticed that code similar to the following doesn't generate errors:

public class Test {

    private static String[][] smallArray = new String[4][4];
    private static String[][] biggerArray = new String[21][21];
    private static String[][] assignedLater;

    public static void main(String args[]){
        for(int i = 0; i < smallArray.length; i++){
            for(int j = 0; j < smallArray[0].length; j++){
                smallArray[i][j] = i + j + "";
            }
        }
        for(int i = 0; i < biggerArray.length; i++){
            for(int j = 0; j < biggerArray[0].length; j++){
                biggerArray[i][j] = i + j + "";
            }
        }
        assignedLater = smallArray;
        //last element of last row
        System.out.println(assignedLater[3][3]) //returns 6
        assignedLater = biggerArray;
        //new last element of new last row
        System.out.println(assignedLater[20][20]) //returns 40
    }
}

After playing around with this for a bit, I ended up testing the following:

public class Test {

    private static String[][] smallArray = new String[2][4];
    private static String[][] biggerArray = new String[2][21];
    private static String[][] errorArray = new String[3][21];
    private static String[][] assignedLater = new String[2][0];

    public static void main(String args[]){
        //fill arrays as in previous example
        assignedLater = smallArray;
        //last element of last row
        System.out.println(assignedLater[1][3]) //returns 6
        assignedLater = biggerArray;
        //new last element of new last row
        System.out.println(assignedLater[1][20]) //returns 21
        assignedLater = errorArray; //no error
        System.out.println(assignedLater[2][20]); // returns 22
    }
}

What's going on here?

Edit Thanks for the responses, I am hereby enlightened as to why the impossible isn't actually happening.

1
  • assignedLater is not an array. It is a reference to an array.
    – assylias
    Aug 9, 2013 at 15:37

4 Answers 4

7

You are not changing the size of arrays. You are just changing the reference to point to a different array in memory.

3
  • So this would work even if assignedLater had already been given a size? Aug 9, 2013 at 15:41
  • Yes, it would. assignedLater is not the array itself but rather a reference to the array.
    – sushain97
    Aug 9, 2013 at 15:43
  • assignedLater is simply a string 2D array reference, it can be assigned an object of any 2D array. Aug 9, 2013 at 15:43
0

The arrays do not change size at all, what you are doing is making the varibles change reference, they will simply change which array they point to without those arrays changing size.

0

As Juned said you're just changing the reference so you don't need such a complicated example to demonstrate. Even this will suffice:

int[] smallArr = new int[5];
int[] bigArr = new int[5000];
smallArr = bigArr;
System.out.println(smallArr.length); // -> prints 5000... 

but we still haven't changed the length of a static array, just changed the location to which smallArr refers to be where bigArr refers.

0

In your second block, when you have this line, you gave 'assignedLater' some dimensions:

private static String[][] assignedLater = new String[2][0];

But then, in your main function, you pointed it to different arrays, so the old dimensions no longer matter:

assignedLater = smallArray;

After this, any call to assignedLater indices will point to smallArray indices. (until you reassign it again later). And so on.

2

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.