int[] a = {1,1,1,2,2,3,4,4,4,4,4,4};写一个方法根据a[]里面的值撤分成多个int[]
结果:
b[]={1,1,1};
c[]={2,2};
d[]={3};
e[]={4,4,4,4,4,4};

解决方案 »

  1.   


    class Ju {    public static void main(String[] args) {
            int[] b = {1,1,1,2,2,3,4,4,4,4,4,4}; 
            int []a=new int[b.length+1];
            for (int i = 0; i < b.length; i++) {
                a[i]=b[i];
            }
            a[b.length]=b[b.length-1]+1;
            int count=1;
            int temp=a[0];
            for (int i = 1; i < a.length; i++) {
               if(a[i]==temp){
                   count++;
               }else{
                   int []ab=new int[count];
                   for (int j = 0; j < ab.length; j++) {
                      ab[j]=temp;
                       System.out.print(ab[j]+" ");
                   }
                   temp=a[i];
                   count=1;
                   System.out.println("");
               }
            }
        }
    }一个很麻烦,很烂的写法.
      

  2.   


    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;public class Test
    {
        public static void main(String[] args)
        {
            LinkedHashMap<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
            int[] a = {1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4};
            
            for(int n: a)
            {
                if(map.containsKey(n))
                {
                    map.put(n, map.get(n) + 1);
                }
                else
                {
                    map.put(n, 1);
                }
            }
            
            List<List<Integer>> list = new ArrayList<List<Integer>>();
            List<Integer> subList;
            
            for(int key: map.keySet())
            {
                subList = new ArrayList<Integer>();
                
                for(int i = 0; i < map.get(key); i++)
                {
                    subList.add(key);
                }
                
                list.add(subList);
            }
            
            for(List<Integer> outList: list)
            {
                for(int num: outList)
                {
                    System.out.print(num + "  ");
                }
                
                System.out.println();
            }
            
            
            /*运行结果
             
            1  1  1  
            2  2  
            3  
            4  4  4  4  4  4  
            
            */
        }
    }
      

  3.   

    int[] a = {1,1,1,2,2,3,4,4,4,4,4,4}; 
    用一个变量temp标记,一开始设置为a[0],也就是1.循环往后比较,并记录temp值出现的次数,当比较到2的时候,这时候确定1已经数完了,得到第一个数组.然后设置temp为2,继续往下数....
    一开始复制数组主要是为了设置哨兵节点方便处理.
      

  4.   

    public class ArraySpliter { public static int[][] splitArray(int[] a) {
    if (a == null) return null;
    if (a.length == 0) return new int[0][];
    a = Arrays.copyOf(a, a.length + 1);  //准备好 a 的副本
    Arrays.sort(a, 0, a.length - 1);  //先排好序
    a[a.length - 1] = a[a.length - 2] + 1;
    List<int[]> list = new LinkedList<int[]>();
    int len = a.length - 1;
    for (int i = 0, j = 0; i < len;) {
    if (a[i] == a[++i]) continue;
    list.add(Arrays.copyOfRange(a, j, i));
    j = i;
    }
    return list.toArray(new int[list.size()][]);
    } public static void main(String[] args) {
    int[] a = {1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4};
    int[][] r = splitArray(a);
    for (int[] ia : r) {
    for (int i : ia) {
    System.out.print(i + " ");
    }
    System.out.println();
    }
    }}
      

  5.   


    public static void main(String[] args) 
    {
    int[] a = {1,1,1,2,2,3,4,4,4,4,4,4}; List tmpList=splitArray(a); 
    for(Iterator i=tmpList.iterator();i.hasNext();)
    {
    int[] tmpArr=(int[])i.next();
    for(int j=0;j<tmpArr.length;j++)
    {
    System.out.print(tmpArr[j]);
    }
    System.out.println();
    }
    }public static List splitArray(int[] orgArr)
    {
    List returnList=new ArrayList();
    int i=0;
    while(i<orgArr.length)
    {
    int iValue=orgArr[i];
    int j=i+1;
    for(;j<orgArr.length;j++)
    {
    if(orgArr[j]!=iValue)
    break;
    }
    int[] tmpArr=new int[j-i];
    System.arraycopy(orgArr, i, tmpArr, 0, tmpArr.length);
    returnList.add(tmpArr);
    i=j;
    } return returnList;
    }
      

  6.   


    public class Test{

    private static void test(int[] a){
    ArrayList al = new ArrayList();
    int temp = 0;
    int breakout = a.length;
    int i = 0;
    int j;
    while(true){
    temp = a[i];
    for(;i < a.length;i++){
    if(temp == a[i])
    al.add(a[i]);
    else break;

    }
    for(j = 0;j < al.size();j++){
    System.out.print(al.get(j)+" ");

    }
    System.out.println();
    al.clear();
    if(breakout == i) break;
    }
    }
    public static void main(String[] args){
    int[] a = {1,1,1,2,2,3,4,4,4,4,4,4};
    test(a);
    }
    }
      

  7.   

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedList;
    import java.util.List;public class ArraySpliter { /**
     * 利用了 JRE1.6 的新方法。
     * @param a 待分割的一维数组
     * @return 分割好的二维数组
     */
    public static int[][] splitArray(int[] a) {
    if (a == null) return null;
    if (a.length == 0) return new int[0][];
    a = Arrays.copyOf(a, a.length + 1);  //准备好 a 的副本
    Arrays.sort(a, 0, a.length - 1);  //先排好序
    a[a.length - 1] = a[a.length - 2] + 1;
    List<int[]> list = new LinkedList<int[]>();
    int len = a.length - 1;
    for (int i = 0, j = 0; i < len;) {
    if (a[i] == a[++i]) continue;
    list.add(Arrays.copyOfRange(a, j, i));
    j = i;
    }
    return list.toArray(new int[list.size()][]);
    } /**
     * 兼容 JRE1.5。
     * @param a 待分割的一维数组
     * @return 分割好的二维数组
     */
    public static int[][] splitIntArray(int[] a) {
    if (a == null) return null;
    if (a.length == 0) return new int[0][];
    List<int[]> list = new ArrayList<int[]>(a.length);  //作最坏的打算
    System.arraycopy(a, 0, a = new int[a.length + 1], 0, a.length - 1);  //准备好 a 的副本
    Arrays.sort(a, 0, a.length - 1);  //先排好序
    a[a.length - 1] = a[a.length - 2] + 1;
    int len = a.length - 1;
    for (int i = 0, j = 0; i < len;) {
    if (a[i] == a[++i]) continue;
    int[] ia = new int[i - j];
    System.arraycopy(a, j, ia, 0, ia.length);
    list.add(ia);
    j = i;
    }
    return list.toArray(new int[list.size()][]);
    } public static void main(String[] args) {
    int[] a = {0, 1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4, -3};
    int[][] r = splitIntArray(a);
    for (int[] ia : r) {
    for (int i : ia) {
    System.out.print(i + " ");
    }
    System.out.println();
    }
    }}输出结果:-3 

    1 1 1 
    2 2 

    4 4 4 4 4 4 
      

  8.   

    我换了一种思路来想这个问题,虽然只是半成品,希望大家觉得对开阔思路方面有点帮助。
    /**
     * 
     */
    package com.test;import java.util.HashMap;
    import java.util.Iterator;
    /**
     * @author zhameng
     *
     */
    public class SplitArray {

    /**
     * 功能:把一个整型数组根据内容拆分成N个内容相同的数组
     * 创建时间:2008年7月24日
     * 
     * @param a  整型数组
     */
    public static void getArray(int[] a){
    HashMap hashmap = new HashMap();
    for(int i = 0; i < a.length; i++){
    if(hashmap.get(a[i]+"")==null){  //  如果值不存在于数组中,增加该值
    hashmap.put(a[i]+"", "0");
    }else{  //  如果值不存在于数组中,增加该值的个数
    int count = Integer.parseInt((String)hashmap.get(a[i]+""))+1;
    hashmap.put(a[i]+"", count+"");
    }
    }
    // 一下步骤用于显示,可以转化为题目所需要的数组
    for(Iterator it = hashmap.keySet().iterator(); it.hasNext();){
    String key = (String)it.next();
    for(int i = 0; i <= Integer.parseInt((String)hashmap.get(key)); i++){
    System.out.print(key+" ");
    }
    System.out.println("");
    }
    } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO 自动生成方法存根
    int[] a = {1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4};
    getArray(a);
    }}
      

  9.   

    上个方法运行的结果是

    2 2 
    1 1 1 
    4 4 4 4 4 4 虽然要求得到的是数组集,不过在不知道数组长度的时候初始化很麻烦,所以我绕了点路,用hashmap来获得值,可能方法不是很好,不过我想毕竟是一条路。
      

  10.   

    int[] a = {1,1,1,2,2,3,4,4,4,4,4,4}; 
    用一个变量temp标记,一开始设置为a[0],也就是1.循环往后比较,并记录temp值出现的次数,当比较到2的时候,这时候确定1已经数完了,得到第一个数组.然后设置temp为2,继续往下数.... 
    一开始复制数组主要是为了设置哨兵节点方便处理.
    具体思想就是这么一个思路