有一个数组String[],其String[1]=001,String[2]=001,String[3]=002,……,想将String[1],String[2]项去除一项,剩一个就行

解决方案 »

  1.   


        public static void main(String[] args) throws Exception
        {
            String[] a = new String[]{"001", "002", "003", "001"};
            
            HashMap<String, String> map = new HashMap<String, String>();
            for(String b : a)
            {
                map.put(b, "");
            }
            
            Object[] c = map.keySet().toArray();
            String[] a1 = new String[c.length];
            System.arraycopy(c, 0, a1, 0, c.length);
            
            for(String b : a1)
            {
                System.out.println(b);
            }
        }
      

  2.   

    不好意思,刚才那个是1.5的,1.4这样写    public static void main(String[] args) throws Exception
        {
            String[] a = new String[]{"001", "002", "003", "001"};
            
            HashMap map = new HashMap();
            for(int i = 0; i < a.length; i++)
            {
                map.put(a[i], "");
            }
            
            Object[] c = map.keySet().toArray();
            String[] a1 = new String[c.length];
            System.arraycopy(c, 0, a1, 0, c.length);
            
            for(int i = 0; i < c.length; i++)
            {
                System.out.println(c[i]);
            }
        }
      

  3.   

    你直接往Set里一扔就可以了  不用管其他的了
      

  4.   

    用TreeSet
    import java.util.*;public class TestString {
    static String[] a= {"001", "002","001","003", "001"};
    static TreeSet ts=new TreeSet();
    public static void main(String[] args){

    for(int i=0;i<a.length;i++){
    ts.add(a[i]);
    }
    System.out.println(ts);
    }
    }
      

  5.   

    其实用HashSet也可以只是默认的不是排好序的
    import java.util.*;
    public class Test{  public static void main(String[] args){
        String s[] = {"001","001","002","003","004"};
        HashSet hs = new HashSet();
        for(int i=0;i<s.length;i++){
          hs.add(a[i]);
        }   
        System.out.println(hs);
      }}
      

  6.   

    如果不想改变元素在原始数组中出现的顺序,那就用下面方法:
    import java.util.ArrayList;
    import java.util.List;public class Distinct { public static String[] distinct(String[] sa) {
    if (sa == null) throw new NullPointerException("sa is null");
    if (sa.length == 0) return new String[0];
    List list = new ArrayList(sa.length);
    for (int i = 0, LEN = sa.length; i < LEN;) {
    String ts = sa[i++];
    if (list.contains(ts)) continue;
    list.add(ts);
    }
    sa = new String[list.size()];
    for (int i = 0, LEN = list.size(); i < LEN; i++) {
    sa[i] = (String) list.get(i);
    }
    return sa;
    } public static void main(String[] args) {
    String[] sa = {
    "001", "001", "000", "002", "003",
    "000", "100", "101", "102", "101",
    "106", "000", "105", "110", "112"
    };
    for (int i = 0; i < sa.length;) System.out.print(sa[i++] + " ");
    sa = distinct(sa);
    System.out.println();
    for (int i = 0; i < sa.length;) System.out.print(sa[i++] + " ");
    }}
    输出:
    001 001 000 002 003 000 100 101 102 101 106 000 105 110 112 
    001 000 002 003 100 101 102 106 105 110 112 
      

  7.   

    不知道JDK1.4中是不是就有LinkedHashSet.如果要有的话,用那个最好了.
      

  8.   

    3楼的意思可能是这样吧:
    我找了一个相对来说最简单的方法:
    String[] s = request.getParameterValues("noattRid");
    List list = Arrays.asList(s);
    Set set = new HashSet(list);
    String[] rid=(String [])set.toArray();