[JAVA] String 배열 정렬
2008. 8. 29. 20:14ㆍ컴퓨터 프로그램/JAVA
public class CompareEx
{
public static void main(String args[])
{
String[] str = {"aaa","bbb","ddd","ccc"};
String temp;
for(int i=0; i<str.length; i++)
{
for(int j=i+1; j<str.length; j++)
{
if(str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
for (String ch:str)
{
System.out.println(ch);
}
}
}
{
public static void main(String args[])
{
String[] str = {"aaa","bbb","ddd","ccc"};
String temp;
for(int i=0; i<str.length; i++)
{
for(int j=i+1; j<str.length; j++)
{
if(str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
for (String ch:str)
{
System.out.println(ch);
}
}
}