对一个数组的处理。
来源:互联网 发布:航天开票软件安装 编辑:程序博客网 时间:2024/06/05 09:05
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的一个回答:
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)); } }}
不知道用python怎么处理?
在想中
网友给出的一个python的解决方法:
def splistarray(array): group = [] item = [] for i in array: if i !=0: item.append(i) elif len(item) >0: group.append(item) item = [] if len(item) > 0: group.append(item) return group
谢谢.
0 0
- 对一个数组的处理。
- JS对数组的处理
- PHP对数组及字符串的处理
- Com对数组的有关处理
- js中对数组的处理
- PHP对数组的常用处理
- memset对int型数组的处理
- 一个菜鸟对OC的积累 -- 数组
- java BigDecimal 对金额的一个处理!
- PHP数组唯一函数array_unique对二维数组的处理
- 对一个数组元素排序,对应的数组元素排序
- 为什么处理一个排序数组要比处理一个未排序的数组更快?
- 对返回数组的存储过程的处理
- java对json数组格式的字符串的处理
- SQL Server存储过程 对数组参数的循环处理
- SQL Server存储过程 对数组参数的循环处理
- SQL Server存储过程 对数组参数的循环处理
- Xstream解析XML,包括对数组和List的处理
- ubunt 12.04 64位 english 服务器版安装
- myeclipse python插件安装和环境配置(Windows7下)
- 只此荒废的一个半个月。
- django1.6 apache 项目部署.
- ubuntu 的 crontab 计划任务
- 对一个数组的处理。
- ubuntu 的SSH 服务
- ubuntu ufw 防火墙的设置
- ubuntu上一些好的操作方式 习惯 文件备份
- django1.6 CSRF verification failed. Request aborted. 用出现表单提交
- django1.6 GET url传参 乱码
- jquery js ajax 不错的想法
- django1.6 django-dajaxice的安装配置.
- django1.6 python manage.py collectstatic