比如循环创建10个名字不同的数组,如何创建?
或者说有一个字符串,他含有多个不同的元素(不同元素的个数不少于10),如何将这些不同的元素存储在不同的数组中?
有代码更好,谢谢了

解决方案 »

  1.   

    这个要用到泛型,你定义一个类,然后这个类可以保存各种类型的属性,再找个泛型集合arraylist把你那个累保存就实现1个数组保存多个类型不同变量了。
      

  2.   

    public class Array{
         
         public void addArr(Vector<int[]> vt){
           int length = getRandomNumber(10, 20);
           int arr[] = new int[length];
           for(int i=0; i<length; i++){
               arr[i] = getRandomNumber(1, 1000);
           }
           vt.add(arr);
         }
         //得到随机数字(包括min 不包括max)
         public static int getRandomNumber(int min, int max) {
    return min + Math.abs(new Random(System.currentTimeMillis()).nextInt()) % (max - min);
         }     public static void main(String str[]){
             Vector<int[]> cc = new Vector<int[]>();    
             Array array = new Array();
             array.addArr(cc);
                   }}
      

  3.   

    String str[]={"asd","1sdf","sfe","asd","ddd","sss","ddd","sss","wer","erw"};也就是如何将这个数组中的不同值取出来并保存。这个str的长度并不确定。每取出一个值先判断是否相同,相同的放在一起,不同的有多少个则创建多少个数组保存。
    这次不知道说清楚了没
      

  4.   


    int length = xxx;
    String[] array = new String[length];
    array[0] = ....
    array[1] = ....
    array[2] = ....
      

  5.   

    Set<String> dataSet = new HashSet<String>();
    for(String aString:ListString)
        dataSet.append(aString);
    System.out.println(dataSet.length);
      

  6.   

    使用 Google guava 提供的Multiset
      

  7.   

    public static void main(String[] args) {
    Map<String, List<String>> map = new HashMap<String, List<String>>();
    String str[]={"asd","1sdf","sfe","asd","ddd","sss","ddd","sss","wer","erw"};
    for(String s : str){
        if(map.get(s) == null){
            List<String> list = new ArrayList<String>();
            list.add(s);
            map.put(s, list);
        }else{
            map.get(s).add(s);
        }
    }
    Set<Entry<String, List<String>>> set = map.entrySet();
    Iterator<Entry<String, List<String>>> iterator = set.iterator();
    while(iterator.hasNext()){
    Entry<String, List<String>> entry = iterator.next();
    List<String> list = entry.getValue();
    //得到数组
    String[] strs = list.toArray(new String[list.size()]);
    }
    }