【发布时间】:2012-05-20 03:13:57
【问题描述】:
我需要将 StringBuffer 数组转换为字符串数组进行排序。 Java中有什么方法吗?
下面是我的代码,我正在尝试按字母顺序对包含第一个字母为大写字母并忽略其他值的值(输入 1)进行排序。
import java.util.*;
public class IPLMatch
{
public static void main(String args[])
{
int input1 = 4;
String input2[] = {"One","two","Three","foUr"}, output[];
StringBuffer[] temp = new StringBuffer[input1];
int j = 0, k = 0;
for(int i = 0; i < input1; i++)
{
boolean isUpperCase = Character.isUpperCase(input2[i].charAt(0));
if(isUpperCase)
{
temp[j++] = new StringBuffer(input2[i]);
}
}
//I need convert my stringbuffer array (temp) to String array for sorting
Arrays.sort(temp);
System.out.print("{");
for(int i = 0; i < temp.length; i++)
{
System.out.print( temp[i]+ ",");
}
System.out.print("\b}");
}
}
【问题讨论】:
-
如果只需要排序,可以为StringBuffer编写自己的Comparator。
-
请阅读FAQ: How To Ask,然后再提出下一个问题。在问题上投入一些精力会得到更好的答案。
-
天哪,代码太乱了。你根本不需要
StringBuffer。您只想根据这些字符串中的第一个大写字符对 字符串数组 进行排序!你想要你的输出为{"One", "Three", "foUr"}吗? -
没有。只有 {One, Three} 并忽略其他值..
-
那么你真正需要的只是过滤而不是排序?
标签: java arrays string sorting stringbuffer