求解一个Java 基础问题!
课程设计
1
List<String> list = new ArrayList<String>();
list的数据
[615, FAULT_SYMPTOM, 1, 615, FAULT_SYMPTOM, 663, 615, FAULT_SYMPTOM, 821, 615, FAULT_SYMPTOM, 1160, 615, FAULT_SYMPTOM, 1161, 615, FAULT_SYMPTOM, 1162, 615, FAULT_SYMPTOM, 1220, 615, FAULT_SYMPTOM, 1241, 615, FAULT_SYMPTOM, 722, 615, FAULT_SYMPTOM, 880, 813, FAULT_SYMPTOM, 1, 813, FAULT_SYMPTOM, 663, 813, FAULT_SYMPTOM, 821, 813, FAULT_SYMPTOM, 1160, 813, FAULT_SYMPTOM, 1161, 813, FAULT_SYMPTOM, 1162, 813, FAULT_SYMPTOM, 1220, 813, FAULT_SYMPTOM, 1241, 813, FAULT_SYMPTOM, 722, 813, FAULT_SYMPTOM, 880, 834, FAULT_SYMPTOM, 1, 834, FAULT_SYMPTOM, 663, 834, FAULT_SYMPTOM, 821, 834, FAULT_SYMPTOM, 1160, 834, FAULT_SYMPTOM, 1161, 834, FAULT_SYMPTOM, 1162, 834, FAULT_SYMPTOM, 1220, 834, FAULT_SYMPTOM, 1241, 834, FAULT_SYMPTOM, 722, 834, FAULT_SYMPTOM, 880]
想把这数据转成下面类似的格式,就是3个一行的
615, FAULT_SYMPTOM, 1 615, FAULT_SYMPTOM, 663 ......
求解决方法,谢谢!(基础不好,勿喷!)
-
好办.思路:对字符串列表集合对3取余,从前到后,每3个数重新作为一个列表,加入到一个最终列表集resList中,实现如下 List<List<String>>resList=new ArrayList<List<String>>(); List<String>res=new ArrayList<String>(); for(int i=0;i<list.size();i++){ ifi(i%3==0){ resList.add(res); res=new ArrayList<String>(); res.add(list[i]) } else{ res.add(list[i]); } } //结果集[[615, FAULT_SYMPTOM, 1],[615, FAULT_SYMPTOM, 1]... ....]
-
public static void main(String[] args) { String a[]={"615", "FAULT_SYMPTOM", "1", "615", "FAULT_SYMPTOM", "615", "615", "FAULT_SYMPTOM", "615", "615", "FAULT_SYMPTOM", "615", "615", "FAULT_SYMPTOM"}; List<String> list=Arrays.asList(a); for(int i=0;i<list.size()-3;i++){ System.out.println(list.get(i)+","+list.get(i+1)+","+list.get(i+2)); } }
发表回复