Generic comparitive array selection sort
Clash 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();
java generics
add a comment |Â
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();
java generics
add a comment |Â
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();
java generics
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();
java generics
asked Apr 4 at 7:04
Jack J
4717
4717
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Apr 4 at 7:27
Imus
3,328223
3,328223
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password