String name = "11|12|13|14|15|12|16|14|17";有什么方法可以将它的重复值去掉;(12和14重复)得到 name = "11|12|13|14|15|16|17";
谢谢

解决方案 »

  1.   

    JDK1.5String name = "11|12|13|14|15|12|16|14|17";
        String[] array = name.split("\\|");
        Set<String> hashSet = new LinkedHashSet<String>();
        Collections.addAll(hashSet, array);
        StringBuilder buff = new StringBuilder();
        for (String string : hashSet) {
          buff.append(string).append("|");
        }
        String result = buff.deleteCharAt(buff.length() - 1).toString();
        System.out.println(result);
      

  2.   

    public String run(){
    String name = "11|12|13|14|15|12|16|14|17";
    String arr[]=name.split("|");
    int course[]=new int[100];
    String target="";
    int a;

    for(int i=0;i<arr.length;i++){
    a=Integer.parseInt(arr[i]);

    if(course[a]==0){
    target+=arr[i];
    target+="|";
    course[a]=1;
    }
    }
    return target;
    }
      

  3.   

    JDK 1.4
    String name = "11|12|13|14|15|12|16|14|17";
        String[] array = name.split("\\|");
        Set hashSet = new HashSet();
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
          if (hashSet.add(array[i])) { // Set的add方法在该元素已经存在时返回false
            buff.append(array[i]).append("|");
          }
        }
        
        String result = buff.deleteCharAt(buff.length() - 1).toString();
        System.out.println(result);