Generic comparitive array selection sort

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












My code works, but my IDE is giving me warnings for unchecked method invocation. I have found useful information about Comparable arrays here. I have found useful information about unchecked method invocation here. Despite this, I just can't figure out how to please my IDE. My intention is to have the method selectionSort sort an array of either Integers or Doubles using compareTo.



import java.util.Arrays;

class Main
public static void main(String args)

Comparable data0 = 2, 8, -39, 904, 8, 0;
Comparable data1 = 1.1, -5.68, 4.39, -0.01, 5.59;

System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data0));
selectionSort(data0);
System.out.printf("%nSorted array: %s%n", Arrays.toString(data0));

System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data1));
selectionSort(data1);
System.out.printf("%nSorted array: %s%n", Arrays.toString(data1));


private static <T extends Comparable<T>> void selectionSort(T data)
for(int i = 0; i < data.length - 1; ++i)
int smallest = i;

for(int index = i + 1; index < data.length; ++index)
if (data[smallest].compareTo(data[index]) > 0)
smallest = index;



swap(data, i, smallest);
printPass(data, i + 1, smallest);



private static <T> void swap(T data, int first, int second)
T temporary = data[first];
data[first] = data[second];
data[second] = temporary;


private static <T> void printPass(T data, int pass, int index)
System.out.printf("after pass %2d: ", pass);

for(int i = 0; i < index; ++i)
System.out.printf("%s ", data[i]);


System.out.printf("%s* ", data[index]);

for(int i = index + 1; i < data.length; ++i)
System.out.printf("%s ", data[i]);


System.out.printf("%n ");

for (int j = 0; j < pass; j++)
System.out.printf("-- ");

System.out.println();








share|improve this question

























    up vote
    1
    down vote

    favorite












    My code works, but my IDE is giving me warnings for unchecked method invocation. I have found useful information about Comparable arrays here. I have found useful information about unchecked method invocation here. Despite this, I just can't figure out how to please my IDE. My intention is to have the method selectionSort sort an array of either Integers or Doubles using compareTo.



    import java.util.Arrays;

    class Main
    public static void main(String args)

    Comparable data0 = 2, 8, -39, 904, 8, 0;
    Comparable data1 = 1.1, -5.68, 4.39, -0.01, 5.59;

    System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data0));
    selectionSort(data0);
    System.out.printf("%nSorted array: %s%n", Arrays.toString(data0));

    System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data1));
    selectionSort(data1);
    System.out.printf("%nSorted array: %s%n", Arrays.toString(data1));


    private static <T extends Comparable<T>> void selectionSort(T data)
    for(int i = 0; i < data.length - 1; ++i)
    int smallest = i;

    for(int index = i + 1; index < data.length; ++index)
    if (data[smallest].compareTo(data[index]) > 0)
    smallest = index;



    swap(data, i, smallest);
    printPass(data, i + 1, smallest);



    private static <T> void swap(T data, int first, int second)
    T temporary = data[first];
    data[first] = data[second];
    data[second] = temporary;


    private static <T> void printPass(T data, int pass, int index)
    System.out.printf("after pass %2d: ", pass);

    for(int i = 0; i < index; ++i)
    System.out.printf("%s ", data[i]);


    System.out.printf("%s* ", data[index]);

    for(int i = index + 1; i < data.length; ++i)
    System.out.printf("%s ", data[i]);


    System.out.printf("%n ");

    for (int j = 0; j < pass; j++)
    System.out.printf("-- ");

    System.out.println();








    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      My code works, but my IDE is giving me warnings for unchecked method invocation. I have found useful information about Comparable arrays here. I have found useful information about unchecked method invocation here. Despite this, I just can't figure out how to please my IDE. My intention is to have the method selectionSort sort an array of either Integers or Doubles using compareTo.



      import java.util.Arrays;

      class Main
      public static void main(String args)

      Comparable data0 = 2, 8, -39, 904, 8, 0;
      Comparable data1 = 1.1, -5.68, 4.39, -0.01, 5.59;

      System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data0));
      selectionSort(data0);
      System.out.printf("%nSorted array: %s%n", Arrays.toString(data0));

      System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data1));
      selectionSort(data1);
      System.out.printf("%nSorted array: %s%n", Arrays.toString(data1));


      private static <T extends Comparable<T>> void selectionSort(T data)
      for(int i = 0; i < data.length - 1; ++i)
      int smallest = i;

      for(int index = i + 1; index < data.length; ++index)
      if (data[smallest].compareTo(data[index]) > 0)
      smallest = index;



      swap(data, i, smallest);
      printPass(data, i + 1, smallest);



      private static <T> void swap(T data, int first, int second)
      T temporary = data[first];
      data[first] = data[second];
      data[second] = temporary;


      private static <T> void printPass(T data, int pass, int index)
      System.out.printf("after pass %2d: ", pass);

      for(int i = 0; i < index; ++i)
      System.out.printf("%s ", data[i]);


      System.out.printf("%s* ", data[index]);

      for(int i = index + 1; i < data.length; ++i)
      System.out.printf("%s ", data[i]);


      System.out.printf("%n ");

      for (int j = 0; j < pass; j++)
      System.out.printf("-- ");

      System.out.println();








      share|improve this question











      My code works, but my IDE is giving me warnings for unchecked method invocation. I have found useful information about Comparable arrays here. I have found useful information about unchecked method invocation here. Despite this, I just can't figure out how to please my IDE. My intention is to have the method selectionSort sort an array of either Integers or Doubles using compareTo.



      import java.util.Arrays;

      class Main
      public static void main(String args)

      Comparable data0 = 2, 8, -39, 904, 8, 0;
      Comparable data1 = 1.1, -5.68, 4.39, -0.01, 5.59;

      System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data0));
      selectionSort(data0);
      System.out.printf("%nSorted array: %s%n", Arrays.toString(data0));

      System.out.printf("Unsorted array: %s%n%n", Arrays.toString(data1));
      selectionSort(data1);
      System.out.printf("%nSorted array: %s%n", Arrays.toString(data1));


      private static <T extends Comparable<T>> void selectionSort(T data)
      for(int i = 0; i < data.length - 1; ++i)
      int smallest = i;

      for(int index = i + 1; index < data.length; ++index)
      if (data[smallest].compareTo(data[index]) > 0)
      smallest = index;



      swap(data, i, smallest);
      printPass(data, i + 1, smallest);



      private static <T> void swap(T data, int first, int second)
      T temporary = data[first];
      data[first] = data[second];
      data[second] = temporary;


      private static <T> void printPass(T data, int pass, int index)
      System.out.printf("after pass %2d: ", pass);

      for(int i = 0; i < index; ++i)
      System.out.printf("%s ", data[i]);


      System.out.printf("%s* ", data[index]);

      for(int i = index + 1; i < data.length; ++i)
      System.out.printf("%s ", data[i]);


      System.out.printf("%n ");

      for (int j = 0; j < pass; j++)
      System.out.printf("-- ");

      System.out.println();










      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 4 at 7:04









      Jack J

      4717




      4717




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          When I put this into IntelliJ with my default settings I already get a warning on the Comparable data0 declaration.




          Raw use of parameterized class




          Changing the array declarations to these:



          Integer data0 = 2, 8, -39, 904, 8, 0;
          Double data1 = 1.1, -5.68, 4.39, -0.01, 5.59;


          solved both warnings.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191219%2fgeneric-comparitive-array-selection-sort%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            When I put this into IntelliJ with my default settings I already get a warning on the Comparable data0 declaration.




            Raw use of parameterized class




            Changing the array declarations to these:



            Integer data0 = 2, 8, -39, 904, 8, 0;
            Double data1 = 1.1, -5.68, 4.39, -0.01, 5.59;


            solved both warnings.






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              When I put this into IntelliJ with my default settings I already get a warning on the Comparable data0 declaration.




              Raw use of parameterized class




              Changing the array declarations to these:



              Integer data0 = 2, 8, -39, 904, 8, 0;
              Double data1 = 1.1, -5.68, 4.39, -0.01, 5.59;


              solved both warnings.






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                When I put this into IntelliJ with my default settings I already get a warning on the Comparable data0 declaration.




                Raw use of parameterized class




                Changing the array declarations to these:



                Integer data0 = 2, 8, -39, 904, 8, 0;
                Double data1 = 1.1, -5.68, 4.39, -0.01, 5.59;


                solved both warnings.






                share|improve this answer













                When I put this into IntelliJ with my default settings I already get a warning on the Comparable data0 declaration.




                Raw use of parameterized class




                Changing the array declarations to these:



                Integer data0 = 2, 8, -39, 904, 8, 0;
                Double data1 = 1.1, -5.68, 4.39, -0.01, 5.59;


                solved both warnings.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 4 at 7:27









                Imus

                3,328223




                3,328223






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191219%2fgeneric-comparitive-array-selection-sort%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?