当前位置:编程学习 > JAVA >>

数组分组问题。急???????

--------------------编程问答-------------------- 你的意思是,随机产生数字,然后给他分组,四个数字一组?
你可以产生数字后进行排序,然后分组~~~ --------------------编程问答-------------------- 这其实不是产生4个数组,而是产生4个数字而已。
比如产生
1~8,9~16,17~24 
就是产生1,9,17,24这四个数字而已。 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 结果
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
代码
import java.util.ArrayList;
import java.util.List;

public class t {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub

List<List> result = group(3, 10);
for (List list : result) {
System.out.println(list);
}

}

public static List<List> group(int size, int num) {
int total = num % size == 0 ? num / size : num / size + 1;
List<List> result = new ArrayList<List>();
for (int i = 0; i < total; i++) {
int start = i * size + 1;
List temp = new ArrayList();
int end = start + size <= num ? start + size : num + 1;
for (int j = start; j < end; j++) {
temp.add(j);
}
result.add(temp);
}
return result;
}

}

--------------------编程问答--------------------
引用 2 楼 AA5279AA 的回复:
这其实不是产生4个数组,而是产生4个数字而已。
比如产生
1~8,9~16,17~24 
就是产生1,9,17,24这四个数字而已。
  顶~,知道间隔就能分~~
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,