What's causing this error?
Hi!
I'm writing a simple program to sort my simple list slapped together in the same program. Unfortunately, I'm getting a compile error looking like this:
InsertionSort.java:32: cannot find symbol
symbol : method parseInt(java.lang.Object)
location:class java.lang.Integer
int positionElement = Integer.parseInt(myList.get(position));
^
Note: InsertionSort.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:uncheckedfor details.
1 error
And here's the program:
import java.util.LinkedList;
class InsertionSort{
// Main method
publicstaticvoid main(String[] args){
// Create our fancy linked list
LinkedList myList =new LinkedList();
// Let's add some arbitrary numbers to it
myList.add("13");
myList.add("5");
myList.add("18");
myList.add("7");
System.out.println("The unsorted list: " + myList);
System.out.println("The sorted list: " + myList);
// Test
int size = myList.size();
int marked = 0;
int position = 0;
for (position = 0; position < size; position++){
marked = position;
int positionElement = Integer.parseInt(myList.get(position));
while (positionElement > (positionElement + 1)){
position++;
if (positionElement > (positionElement + 1)){
myList.add(position, positionElement);
}
}
position = marked;
}
}
}
Any reason you don't use Collections.sort(myList)? You can still do integer type comparisons on the strings by using a comparator, something like this:
Collections.sort( myList, new Comparator() {
public int compareTo( Object o1, Object o2 ) {
return Integer.parseInt(o1.toString) - Integer.parseInt(o2.toString);
}
public boolean equals(Object o1, Object o2 ) {
return Integer.parseInt(o1.toString) == Integer.parseInt(o2.toString);
}
});
Jemiah
Here is the C implementation. Now all you need to do is port it to java using ArrayList:
void insertionSort(int numbers[], int array_size)
{
int i, j, index;
for (i=1; i < array_size; i++)
{
index = numbers[i];
j = i;
while ((j > 0) && (numbers[j-1] > index))
{
numbers[j] = numbers[j-1];
j = j - 1;
}
numbers[j] = index;
}
}
Well, unfortunately it has to be a Linked List (for reason unknown :/).
Anyway, this is the code as of now. However, instead of arranging the numbers in the correct order, it orders them like this: [13, 5, 5, 5, 5, 5, 5, 5, 18, 7, 4, 6, 12]. And this is completely wrong...
Here's the code:
import java.util.LinkedList;
class InsertionSort {
// Main method
public static void main(String[] args) {
// Create our fancy linked list
LinkedList<Integer> myList = new LinkedList<Integer>();
// Let's add some arbitrary numbers to it
myList.add(13);
myList.add(5);
myList.add(18);
myList.add(7);
myList.add(4);
myList.add(6);
myList.add(12);
System.out.println("The unsorted list: " + myList);
// Let's get down to business...
int size = myList.size();
int position;
int target;
if (myList != null) {
for (position = 1; position < size; position++) {
target = position;
while ((myList.get(position)) >= (myList.get(target))) {
target++;
}
myList.add(target, myList.get(position));
}
}
System.out.println("The sorted list: " + myList);
}
}