germantom
germantom

Reputation: 415

How to increment the size of an array within a loop

I have this bubblesort code that i'm performing a runtime analysis on recording the time it takes to sort the array. I was wondering if there is any way i could increment the size of the array using a loop? Because at the moment i am incrementing it 100 at a time manually and i need to reach an array size of 5000.

public class BubbleSortworking{
public static void main (String[] args) {
    Random rand = new Random();
    int myArray[] = new int[100];  //How to increment this using a loop
    int count, count2;
    count2 = 2;   //amount of times to run the loop

    //repeats the bubble sort, while also producing new arrays each time
    for (count = 0; count < count2; count++){
        for (int i = 0; i < myArray.length; i++){

            myArray[i] = rand.nextInt(100) + 1;  //produce numbers between 1 - ?
            //System.out.print(myArray[i] + ", ");   //displays unsorted array
        }

        bubble(myArray);

        // uncomment below 2 lines to prove each new sorted array cycle is unique 
        //for (int i = 0; i < myArray.length; i++)
        //  System.out.print(myArray[i] + ", ");
    }
}

public static void bubble(int myArray[]){
    int temp;
    long start = System.nanoTime();
    //System.out.println("start " + start);

    //for (count = 0; count < count2; count++){
    for (int i=0; i < myArray.length - 1; i++) {
        for(int j=myArray.length - 1; j > i; j--) {
            if (myArray[j] < myArray[j-1]){
                temp = myArray[j];
                myArray[j] = myArray[j-1];
                myArray[j-1] = temp;
            }
        }
    }

    long end = System.nanoTime();
    System.out.println(end - start);
    //System.out.println("elapsed time " + (end - start));


}

}

Upvotes: 0

Views: 6878

Answers (4)

vels4j
vels4j

Reputation: 11298

This may help you

int[] intA = new int[100];
int sizeToIncrement = 100;
for(int i=0;i<5000;i++) {
   if(i== intA.length ) {
      intA = Arrays.copyOf(intA, intA.length + sizeToIncrement);
   }
   intA[i] = i;
}

Upvotes: 0

Dave W
Dave W

Reputation: 96

The answer by rizon is correct, you cannot change the size of an array. BTW, nowhere are you re-creating the array, nor do I see where you are processing 5000 elements. If you are concerned about processing time, you would not want to recreate/resize an array, as that would be very inefficient. You would want a different solution.

Upvotes: 0

user798182
user798182

Reputation:

You either need to use an ArrayList, which will do this for you but with extra overheads.

Or, you can allocate the array as size 5000 before you start, and record up to how many elements you have used so far in a variable (rather than relying on array.length)

Or, you can resize the array by making a new array which is bigger and copying all the elements to it (System.arrayCopy(..)), as well as putting the new ones in.

Upvotes: 1

rizon
rizon

Reputation: 8197

No you can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size. When it does you'll have to allocate a new one and copy the data from the old to the new.

Upvotes: 1

Related Questions