Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to double the size of an array

I have to implement an array that takes its elements from a random generator. Whenever the array reaches it's last element a method resize() will be called to increase the size of the array. In my code every thing goes alight until invoking resize() method, it does not do any effect on the size of the array. It supposed to increase the size of the array to allow more elements to be entered.

    for (int j=0; j<arr.length-1; j++) {
        random= generator.nextInt(arr.length*4) ;
        arr[j]=random;

        }

    if(arr_size==arr.length){
        resize(arr);
        for (int h=0; h<(arr.length-1)*2; h++) {
            random= generator.nextInt(arr.length*4) ;
            arr[h]=random;}
    }

Here is resize():

     private static void  resize(int[] arr) {

    int[] tmp = new int[2*arr.length];
    System.arraycopy(arr,0,tmp,0,arr.length); 
    arr = tmp;
}
like image 887
Jason Avatar asked Mar 07 '14 14:03

Jason


People also ask

How do you increase the size of an array?

If you want to change the size, you need to create a new array of the desired size, and then copy elements from the old array to the new array, and use the new array. In our example, arr can only hold int values. Arrays can hold primitive values, unlike ArrayList, which can only hold object values.

How do you double an array?

To double the size of array arr of size n, arr[0…n-1]. At first we have to create one new array of size say m. Then copy n elements from arr to the new array. Finally change the value of arr to point to the new array.

How do you increase the size of an array in C++?

Once an array has been allocated, there is no built-in mechanism for resizing it in the C++ programming language. Therefore, we can avoid this problem by dynamically generating a new array, copying over the contents, and then deleting the old array.

Why do we double the size of array?

The reason behind doubling is that it turns repeatedly appending an element into an amortized O(1) operation. Put another way, appending n elements takes O(n) time. More accurately, increasing by any multiplicative factor achieves that, but doubling is a common choice.


1 Answers

Setting arr = tmp does not do what you think it does. You're simply pointing the local variable arr in the resize() method to the local variable tmp. What you want to do is return tmp and assign it to arr outside of the method.

if(arr_size==arr.length){
    arr = resize(arr);

And change the resize() method signature to

private static int[] resize(int[] arr) {
    //Resize
    return tmp;
}

The key thing to take away is that: When passing an object reference to a method in Java, you're passing a copy of that object's reference, which can be thought of as a copy of the location in memory of that object. All such manipulations to this REFERENCE in the called method won't take effect outside of the method. However, you CAN manipulate the actual object itself because, as I said, the reference is pointing to the same place in memory with the exact same object.

But changing a copy of a location in memory to point to a new location in memory does not cause the original calling object reference to point to that same location

like image 150
Kon Avatar answered Sep 22 '22 14:09

Kon