如何在Java中重命名特定字符串数组中的列
String strList [] = new String[]{"a", "b", "a", "c", "b", "b", "B", "A", "","a" };
List<String> resultList = new ArrayList<String>();
for(String str: strList){
if(str == null || str.isEmpty()){
str = "Header";
}
if(isValueContains(str, resultList)){
String [] elements = str.split("_");
String lastElement = elements[elements.length - 1];
int elementInt = getInteger(lastElement);
elementInt++;
resultList.add(str+"_"+elementInt);
}else{
resultList.add(str);
}
}
列应显示为a,b,a1,c,b1,b2,b3,a2等等
答案 0 :(得分:1)
String strList[] = new String[]{"a", "b", "a", "c", "b", "b", "B", "A", "", "a"};
Map<String, Integer> strAppearancesCount = new HashMap<String, Integer>();
List<String> resultList = new ArrayList<String>();
Integer strCount;
for (String str : strList)
{
if (str == null || str.isEmpty())
{
str = "Header";
}
str = str.toLowerCase();
if (strAppearancesCount.containsKey(str))
{
strCount = strAppearancesCount.get(str) + 1;
resultList.add(str + strCount);
}
else
{
resultList.add(str);
strCount = 0;
}
strAppearancesCount.put(str, strCount);
}
System.out.println(resultList);