Hey guys. I am having problems with this code. I am somewhat new to java so this may be a simple mistake on my part. Any help or direction would be greatly appreciated.

I am trying to convert this code to use double instead of int. It is a insertion sort program that sorts the data from a random number generator. I am having issues with the constructor InsertionSort. It warns me that it requires type int and found type double so there will be loss of precision. I do not know why it requires type int. The argument is type double so shouldn't it require type double? Am I missing where int is declared? Thanks in advance for any help!

import java.util.Random;
  import java.lang.*;

  public class InsertionSort
  {
     private double[] data; // array of values
     private static Random generator = new Random();

     // create array of given size and fill with random integers
     public InsertionSort( double size )
     {
        data = new double [ size ]; // create space for array

        // fill array with random ints in range 10-99
        for ( double i = 0; i < size; i++ )
           data[ i ] = 10.00 + generator.nextdouble( 90 );
     } // end InsertionSort constructor




     // sort array using insertion sort
     public void sort()
     {
        double insert; // temporary variable to hold element to insert

        // loop over data.length - 1 elements
        for ( double next = 1; next < data.length; next++ )
        {
           // store value in current element
           insert = data[ next ];

           // initialize location to place element
           double moveItem = next;

           // search for place to put current element
           while ( moveItem > 0 && data[ moveItem - 1 ] > insert )
           {
              // shift element right one slot
              data[ moveItem ] = data[ moveItem - 1 ];
              moveItem--;
           } // end while

           data[ moveItem ] = insert; // place inserted element
           printPass( next, moveItem ); // output pass of algorithm
        } // end for
     } // end method sort

     // print a pass of the algorithm
     public void printPass( double pass, double index )
     {
        System.out.print( String.format( "after pass %2d: ", pass ) );

        // output elements till swapped item
        for ( int i = 0 ; i < index; i++ )
           System.out.print( data[ i ] + "  " );

        System.out.print( data[ index ] + "* " ); // indicate swap

        // finish outputting array
        for ( int i = index + 1; i < data.length; i++ )
           System.out.print( data[ i ] + "  " );

        System.out.print( "\n               " ); // for alignment

        // indicate amount of array that is sorted
        for( int i = 0; i <= pass; i++ )
           System.out.print( "--  " );
        System.out.println( "\n" ); // add endline
     } // end method printPass

     // method to output values in array
     public String toString()
     {
        StringBuffer temporary = new StringBuffer();

        // iterate through array
        for ( double element : data )
           temporary.append( element + "  " );

        temporary.append( "\n" ); // add endline character
        return temporary.toString();
     } // end method toString
  } // end class InsertionSort
public class InsertionSortTest
  {
     public static void main( String[] args )
     {
        // create object to perform selection sort
        InsertionSort sortArray = new InsertionSort( 10.00 );

        System.out.println( "Unsorted array:" );
        System.out.println( sortArray ); // print unsorted array

        sortArray.sort(); // sort array

        System.out.println( "Sorted array:" );
        System.out.println( sortArray ); // print sorted array
     } // end main
  } // end class InsertionSortTest

I fixed the constructor but I am still have a loss of precision issue on line 16.

import java.util.Random;
  import java.lang.*;

  public class InsertionSort
  {
     private double[] data; // array of values
     private static Random generator = new Random();

     // create array of given size and fill with random integers
     public InsertionSort( double size )
     {
        data = new double[] { size }; // create space for array

        // fill array with random ints in range 10-99
        for ( double i = 0.0; i < size; i++ )
           data[ i ] = 10.00 + generator.nextdouble( 90.00 );
     } // end InsertionSort constructor




     // sort array using insertion sort
     public void sort()
     {
        double insert; // temporary variable to hold element to insert

        // loop over data.length - 1 elements
        for ( double next = 1.0; next < data.length; next++ )
        {
           // store value in current element
           insert = data[ next ];

           // initialize location to place element
           double moveItem = next;

           // search for place to put current element
           while ( moveItem > 0 && data[ moveItem - 1 ] > insert )
           {
              // shift element right one slot
              data[ moveItem ] = data[ moveItem - 1 ];
              moveItem--;
           } // end while

           data[ moveItem ] = insert; // place inserted element
           printPass( next, moveItem ); // output pass of algorithm
        } // end for
     } // end method sort

     // print a pass of the algorithm
     public void printPass( double pass, double index )
     {
        System.out.print( String.format( "after pass %2d: ", pass ) );

        // output elements till swapped item
        for ( int i = 0 ; i < index; i++ )
           System.out.print( data[ i ] + "  " );

        System.out.print( data[ index ] + "* " ); // indicate swap

        // finish outputting array
        for ( int i = index + 1; i < data.length; i++ )
           System.out.print( data[ i ] + "  " );

        System.out.print( "\n               " ); // for alignment

        // indicate amount of array that is sorted
        for( int i = 0; i <= pass; i++ )
           System.out.print( "--  " );
        System.out.println( "\n" ); // add endline
     } // end method printPass

     // method to output values in array
     public String toString()
     {
        StringBuffer temporary = new StringBuffer();

        // iterate through array
        for ( double element : data )
           temporary.append( element + "  " );

        temporary.append( "\n" ); // add endline character
        return temporary.toString();
     } // end method toString
  } // end class InsertionSort

The size argument in your constructor should be an int. This is because, the size of an array uses int. In addition, in your for loops (constructor and sort() method), you should be using int as the indexer. Again, arrays use int to reference their index. As well, floating point numbers occasionally given unexpected results like 4.32 + 3.68 = 7.999999997 (this particular example does not actually in this case, but have had similar results in the past).

Line 12. Array sizes and indexes must be int or convertable to int (byte, char). They can't be long.

The type of each dimension expression within a DimExpr must be a type that is convertible (§5.1.8) to an integral type, or a compile-time error occurs. Each expression undergoes unary numeric promotion (§). The promoted type must be int, or a compile-time error occurs; this means, specifically, that the type of a dimension expression must not be long.

(Java Lang Ref 15.10 Array Creation Expressions)

So arrays cannot use double values? Since I want to convert the sort to use double values, I should convert the array to a linked list? Then I could use the collections class to sort. Would that be the best way? Thanks again guys!

Arrays can certainly use double values. However, to reference a double value in the array, or declare its size, you use an int. This is because, you refer to each location as 0, 1, 2, etc.

For example:

double[] a = new double[] {0.1, 0.2, 0.3};
double[] b = new double[] {0.2, 0.4, 0.5};
int length = a.length;
double[] c = new double[length];
for(int i = 0; i < length; i++) {
    c[i] = a[i] + b[i];
}

would work. Just notice that I gave c a length of type int, and referenced the arrays using the int i, but I was using a array of doubles. Let me know if this clears things up a bit, or if it needs to be any clearer.

Thank you very much for your help! That makes perfect since!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.