Hi, all.
Got a selection sort from a previous programming class for plain old arrays that
I've been trying to translate to array lists, but I'm missing key info on toArray
or an easy way to copy the fields from one to another.
I've gotten this far playing around, and it seems to be comparing all, but the
way to copy to the tempArray and display doesn't work, or it's only storing
the first field because the swap/copy part isn't quite right.
I've looked at this too much...I know I'm not seeing the obvious.
Got a selection sort from a previous programming class for plain old arrays that
I've been trying to translate to array lists, but I'm missing key info on toArray
or an easy way to copy the fields from one to another.
Code:
int minIndex;
int minItem;
int start = 0;
minIndex = start;
for (int i = 0; i < array.length-1; i++)
{
//minIndex = findMinIndex(array, i);
for (int index = start+1; index < array.length; index++)
{
if (array[index] < array[minIndex])
minIndex = index;
}
//put the smaller item in place of the current item
minItem = array[minIndex];
array[minIndex] = array[i];
array[i] = minItem;
System.out.println("Array after pass " + (i+1) + " of selection sort:");
for (int j = 0; j < array.length; j++)
System.out.print(array[j] + " ");
System.out.println("");
}//end Selection sort code
I've gotten this far playing around, and it seems to be comparing all, but the
way to copy to the tempArray and display doesn't work, or it's only storing
the first field because the swap/copy part isn't quite right.
Code:
public static void sortList (Scanner keyboard, ArrayList<Player> my_team)throws Exception
{
int minIndex;
int minItem;
int start = 0;
minIndex = start;
ArrayList<Player> tempArray = new ArrayList<Player>();
//Comparable.class.
//for (Player member : my_team)
for (int i = 0; i < my_team.size()-1; i++)
{
//System.out.println(my_team.get(minIndex).getLastName());
for (int index = start+1; index < my_team.size(); index++)
{
if (my_team.get(index).getLastName().compareTo(my_team.get(minIndex).getLastName())<0)
{
String compare_one = my_team.get(index).getLastName();
String compare_two = my_team.get(minIndex).getLastName();
System.out.println(compare_one + " is < " + compare_two + " = "
+ compare_one.compareToIgnoreCase(compare_two));
minIndex = index;
//System.out.println(my_team.get(index).getLastName());
}
//minItem = array[minIndex];
//tempArray.set(i, my_team.get(minIndex));
//System.out.println(my_team.get(minIndex));
//array[minIndex] = array[i];
tempArray.addAll(minIndex, my_team);
//array[i] = minItem;
//System.out.println("Array after pass " + (i+1) + " of selection sort:");
//System.out.println(my_team.get(index).getLastName());
}
System.out.println(tempArray.get(i));
I've looked at this too much...I know I'm not seeing the obvious.