对一个数组的处理。

时间:2014-03-05 18:15:38   收藏:0   阅读:423
array =[0,2,0,0,34,4,3,2,0,0,0,0,4,2,3,0,0]

如上一个数组,一0为间隔 分成几组。

比如说2 在一个组里  34,4,3,2里   4,2,3 在一个组里,请问用JAVA代码怎么处理一下。

 

网友java的一个回答:

bubuko.com,布布扣
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class $ {
    public static void main(String[] args) {
 
        int[] arr = { 0, 2, 0, 0, 34, 4, 3, 2, 0, 0, 0, 0, 4, 2, 3, 0, 0 };
 
        List<List<Integer>> result = new ArrayList<List<Integer>>();
 
        List<Integer> data = null;
 
        for (int i : arr) {
            if (i == 0) {
                if (data != null) {
                    result.add(data);
                }
                data = null;
                continue;
            }
            if (data == null) {
                data = new ArrayList<Integer>();
            }
            data.add(i);
        }
 
        System.out.println(result);
 
        // 到这就可以了,若你想转化为数组,可以用后面的代码
        for (List<Integer> tmp : result) {
            Integer[] a = tmp.toArray(new Integer[0]);
            System.out.println(Arrays.toString(a));
        }
    }
}
bubuko.com,布布扣

 

不知道用python怎么处理?

在想中

对一个数组的处理。,布布扣,bubuko.com

原文:http://www.cnblogs.com/Jghost/p/3580764.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!