Can we change the size of an array at run time?

Can we change the size of an array at run time?

Add Comment


  • 4 Answer(s)

    No we cannot change the array size. Though there are similar data types available which allow a change in size. Well, you can dynamically allocate the size:

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      int *array;
      int cnt;
      int i;
    
      /* In the real world, you should do a lot more error checking than this */
      printf("enter the amount\n");
      scanf("%d", &cnt);
    
      array = malloc(cnt * sizeof(int));
    
      /* do stuff with it */
      for(i=0; i < cnt; i++)
        array[i] = 10*i;
    
      for(i=0; i < cnt; i++)
        printf("array[%d] = %d\n", i, array[i]);
    
      free(array);
    
      return 0;
    }
    Answered on February 18, 2019.
    Add Comment

    No, you cannot change the size of an array during run-time. In Java, we can have two options to transfer bulk objects in between method calls; either as arguments or return values. The Java array type is good in simple cases but it may not be suitable for many cases in real-time projects

    Answered on February 18, 2019.
    Add Comment

    You can not change the size of your array at run time. An alternative is to create a new array which is larger than the existing one. Copy the elements of the existing array to the new array and delete the existing array. And Change the member variables, ptr and size.

    Something like this:

    int* newArray = new int[sizeOfArray];
    std::copy(oldArray, oldArray + std::min(sizeofOldArray, sizeOfArray), newArray);
    delete[] oldArray;
    oldArray = newArray;
    Answered on February 19, 2019.
    Add Comment

    No, you cannot change the size of an array during run-time.

    In Java, we can have two options to transfer bulk objects in between method calls; either as arguments or return values.

    1. By Java array (Ex. Employee employees[] =…)
    2. By Java Collection object (Ex. ArrayList employees = … )

    The Java array type is good in simple cases but it may not be suitable for many cases in real-time projects.

    Case Study : Sorting

    Consider an application that requires sorting in your page where we can easily sort java.util.Collection by calling simple method call. But in the case of array type we need to either rely on database query for sorting or in your Java algorithm .

    This type of design may impact your application’s performance since you need to call again for your back-end for sorting.

    Case Study: Key Sorting

    Consider another use case where you need to store key-value pair where keys alone will be sorted for UI display. We can use HaspMap to store key-value pair and keys can be accessed by keySet() method.

    The collection util package usages/advantages:

    • Easy indexing
    • Easy to resize (arrays cannot be resized once created)
    • Easy sorting
    • Delete/Add an object can be dynamic
    • Key-Value pair type collections
    • Easy to transfer between application tiers
    • Easy to display in JSF/JSP pages by simple tags

    Real time example:

    We have used HashMap to fetch random questions for MyExamCloud exams.

    You can take a look on Java Certifications MyExamCloud Exam Collections and understand about how MyExamCloud Question objects are fetched when a user take a test.

    Answered on February 21, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.