2016-05-06 10 views
-4

die Spalt in bestimmten String-Array benennen, wie die Säulen in bestimmtem String-Array in JavaWie in Java

sollte
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); 
     } 

    } 

die Spalte angezeigt wird umbenannt als a, b, a1, c, b1, b2, b3, a2 und so weiter

Antwort

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);