3

I need to change the size of an array, but I cannot simply create another - It needs the same name so I can pass it to a method. Specifically, I need the array to have twice as many terms as it used to. Is this possible to do with one array? Can I copy data from array A to array B, then make A reference the same data as B with A = B; ?

2
  • 5
    Why not use a List instead? Commented Jan 15, 2012 at 11:57
  • It does not need to have the same name to be able to pass it to a method. Just call the method with the new variable as parameter. Commented Jan 15, 2012 at 12:04

7 Answers 7

7

I used the Arrays.copyOf method, like this:

    int myArray[] = {1,2,3};
    myArray = Arrays.copyOf(myArray, myArray.length+1);
    //previous line creates a copy of the array and adds one to the size

    myArray[3] = 12; // assign the new fourth element the value of 12
    //then loop through the array to output each element
    for(int ctr = 0; ctr<myArray.length; ctr++){
        System.out.println(myArray[ctr]);
    }
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, your array variable may reference an array of the same type but different size.

For changing it internally, an ArrayList might be more easy to use.

Comments

2

That's not how arrays in Java work:

int[] anArray;
anArray = new int[10];

for (int i = 0; i < 10; ++i) {
  // ...
}

int[] secondArray = anArray;

// Grow "anArray" via secondArray to 20 please:
secondArray = new int[20]; // No way to alter anArray with this - now two arrays

The closest work around is to make the array the full size when you allocate it, but not use the whole of it until you're ready. I.e. you want to pre-allocate all the space you'll ever need in it.

int[] anArray;
anArray = new int[20];

for (int i = 0; i < 10; ++i) { // Still < 10 here
  // ...
}

int[] secondArray = anArray;
// no need to change the array, it's already big enough

Either that or use one of the many containers provided.

Comments

2

You need to create a new array since those are static in size. Then use srcArray = Arrays.copyOf(srcArray, srcArray.length * 2);.

Alternatively you might want to think about using a list instead of an array. ArrayList is internally backed by an array which will double its size when needed.

1 Comment

did you mean Arrays.copyOf()?
1
int[] a = new int[5];
// fill a
int[] b = Arrays.copyOf(a, 10);

Comments

0

Using ArrayList is better when compared to Array

Comments

0

An array is an Array. Here you can dynamically increase the size of the array, keeping its name. In this instance, it is increasing its size until the sum of its indexes reaches the goal, which is 100.

    int goal = 0;
    int[] arr = {1,2,3};

    int i = 0;
    while (goal<100){
        goal+=arr[i];
        i++;
        int[] tmpArray = new int[arr.length];
        for (int j = 0; j < arr.length; j++) {
            tmpArray[j] = arr[j];
        }
        if(i>=arr.length){
            arr = new int[arr.length+1];
            for (int j = 0; j < tmpArray.length; j++) {
                arr[j] = tmpArray[j];
            }
            arr[arr.length-1] = 1;
        }
    }

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.