### The array doubling algorithm

• Reading in data from input file of unspecified length

• Summary:

 ``` import java.io.*; // Package with the Input/Output methods // on File objects import java.util.Scanner; // Class to read in data types public class MyProg { public static void main( String[] args ) throws IOException { File inpFile = new File ( "PathNameOfInputFile" ); Scanner inp = new Scanner ( inpFile ); // Now you can read data in with: // inp.nextDouble(); // inp.nextInt(); // ... // // You can also TEST if there is data in input with: // inp.hasNextDouble(); // inp.hasNextInt(); // ... } } ```

• Problem Description:

• You are given a data file with an unknown number of input double typed values:

 ``` 1.2 3.4 5.6 .. (# items is unknown) ```

• Write a program that read in the data items and store them inside an array

• We can solve this program with dynamic arrays:

 ``` Open the data file Create an array a of a certain size (any size, we can change it) while ( there is more data in input file ) { x = read next input value if ( array a is full ) then increase the size of array a store x into the next element of array a } ```

• Array doubling

• A popular strategy to increase the size of an array is:

• Array doubling

 When an array if full and we need to increase its size, then we make the new size equal to twice its original size

In other words: each time we need to increase the array size, the array will double in size.

• Pseudo code:

 ``` Open the input file inp double[] a = new double[1]; // Start with an array // of 1 element n = 0; // n counts the number of items in array // (to detect if array is full or not) while ( inp.hasNextDouble() ) { x = inp.nextDouble(); // Read in next number /* ------------------------------ Check if array has space left ------------------------------ */ if ( n == a.length ) { /* ========================================== Array is full... increase its size first ========================================== */ double[] h; // help variable to make a bigger array h = new double[ 2*a.length ]; // make aray twice as big for ( int i = 0; i < a.length; i++ ) h[i] = a[i]; // copy old values a = h; // make "a" point to new array } /* ========================================== Here, the array will surely have space ========================================== */ a[n] = x; // Put number read in a[n] n++; // Go to next array position } ```

• Java program:

 ``` import java.io.*; import java.util.Scanner; public class ArrayDoubling { public static void main(String[] args) throws IOException { File inpFile = new File( "ArrayDoubling.inp" ); Scanner inp = new Scanner ( inpFile ); double[] a = new double[1]; double x; int n; // Number of items n = 0; while ( inp.hasNextDouble() ) { x = inp.nextDouble(); /* --------------------------------------- Check if array has space to store x --------------------------------------- */ if ( n == a.length ) { /* ========================================== The array double algorithm ========================================== */ double[] h; // help variable to make a bigger array h = new double[ 2*a.length ]; // make aray twice as big for ( int i = 0; i < a.length; i++ ) h[i] = a[i]; // copy old values a = h; // make "a" point to new array } /* ========================================== Here, the array will surely have space ========================================== */ a[n] = x; // Put number read in a[n] n++; // Go to next array position } /* ========================================== Done, print values read ========================================== */ for ( int i = 0; i < a.length; i++ ) System.out.println( a[i] ); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac ArrayDoubling.java To run:          java ArrayDoubling

• Note on array doubling

• The array doubling algorithm is flexible, but creates a lot of garbage

• Each time a new array is created, the old array will become garbage !!!