0
String temp ="";
int Count =0;
String[] names = new String[10];   
for(int a = 0; a<=9; a++)
{

    String name = JOptionPane.showInputDialog("enter name");
    names[a]=name;
    Count++;

}
System.out.println("Unchanged: "+Arrays.toString(names));

for(int i=0;i<(Count-1);i++)
{
for(int j=(i+1); j<Count;j++)
{
    if((names[j].compareTo(names[i]))>0)
    {
        temp = names[i];
        names[i] =names[j];
        names[i]=temp;



    }

}

}

Hi i'm trying to get this manual sort to work (i know there is a quick sort option but i have to do it this way) and i cannot see what the problem is any suggestions?

4

1 に答える 1

2

names[i] と names[j] を入れ替えたい場合は、次のように記述します。

    temp = names[i];
    names[i] = names[j];
    names[j] = temp;      // <--- replaced i with j
于 2013-02-27T14:53:08.860 に答える