Java 如何在for的每个循环中重置arrylist?
我有一个整数数组列表。我希望在每个for中清除它,然后再次填充。我的代码是:Java 如何在for的每个循环中重置arrylist?,java,Java,我有一个整数数组列表。我希望在每个for中清除它,然后再次填充。我的代码是: private static int myMethod(int prim){ ArrayList<ArrayList<Integer>> number = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> sublist = new ArrayList<Integer>
private static int myMethod(int prim){
ArrayList<ArrayList<Integer>> number = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> sublist = new ArrayList<Integer>();
ArrayList<Integer> temp = new ArrayList<Integer>();
for (int x = 2; x < prim; x++ ){
for (int power = 0; power < prim - 1; power++){
// in this loop sublist
//will be fill
int i=(int)((Math.pow(x, power))%prim);
sublist.add(i);
}
number.add(sublist);
}
}
但number arraylist有以下形式:
[[1,2,4,1,2,4,1, 3, 2, 6, 4, 5][1,2,4,1,2,4,1, 3, 2, 6, 4, 5]]
请帮助我:(您不能只清除
子列表
;您必须通过循环每次创建一个新的子列表
for (int x=2;x<prim;x++ ){
sublist = new ArrayList<Integer>();
for (int power=0;power<prim-1;power++){ // in this loop sublist //will be fill
int i=(int)((Math.pow(x, power))%prim);
sublist.add(i);
}
number.add(sublist);
}
for(int x=2;x在for循环中创建列表
ArrayList<ArrayList<Integer>> number=new ArrayList<ArrayList<Integer>>();
for (int x=2;x<prim;x++ ){
// create sublist rather than outside the loop
ArrayList<Integer> sublist = new ArrayList<Integer>();
for (int power=0;power<prim-1;power++){ // in this loop sublist //will be fill
int i=(int)((Math.pow(x, power))%prim);
sublist.add(i);
}
number.add(sublist);
}
ArrayList number=new ArrayList();
对于(int x=2;xArrayList number=new ArrayList();
ArrayList子列表=null;
ArrayList temp=新的ArrayList();
对于(int x=2;xplz以更好的形式书写。根本无法理解。Soooooo谢谢。它帮助我:)如果此答案对您有帮助,请单击旁边的复选框接受它。
ArrayList<ArrayList<Integer>> number=new ArrayList<ArrayList<Integer>>();
for (int x=2;x<prim;x++ ){
// create sublist rather than outside the loop
ArrayList<Integer> sublist = new ArrayList<Integer>();
for (int power=0;power<prim-1;power++){ // in this loop sublist //will be fill
int i=(int)((Math.pow(x, power))%prim);
sublist.add(i);
}
number.add(sublist);
}
ArrayList<ArrayList<Integer>> number=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> sublist=null;
ArrayList<Integer> temp=new ArrayList<Integer>();
for (int x=2;x<prim;x++ ){
sublist=new ArrayList<Integer>();
for (int power=0;power<prim-1;power++){ // in this loop sublist //will be fill
int i=(int)((Math.pow(x, power))%prim);
sublist.add(i);
}
number.add(sublist);
}