写一个方法,将arraylist1={{x1,x2,x3},{x4,x5,x6}}改为arraylist2={{x6,x5,X4},{x3,x2,x1}}
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 03:12:21
写一个方法,将arraylist1={{x1,x2,x3},{x4,x5,x6}}改为arraylist2={{x6,x5,X4},{x3,x2,x1}}
写一个方法,将arraylist1={{x1,x2,x3},{x4,x5,x6}}改为arraylist2={{x6,x5,X4},{x3,x2,x1}}
写一个方法,将arraylist1={{x1,x2,x3},{x4,x5,x6}}改为arraylist2={{x6,x5,X4},{x3,x2,x1}}
private ArrayList revertArrayList(ArrayList al){
ArrayList arrayList = new ArrayList<>(al.size());
for(int k=0;k<al.size();++k){
arrayList.add(0);
}
for(int i=0,j=al.size();i<al.size();++i){
arrayList.set(i,al.get(--j));
}
return arrayList;
}
public void revert(){
ArrayList al = new ArrayList();
ArrayList al1 = new ArrayList();
al1.add(1);
al1.add(2);
al1.add(3);
al1 = revertArrayList(al1);
ArrayList al2 = new ArrayList();
al2.add(4);
al2.add(5);
al2.add(6);
al2 = revertArrayList(al2);
al.add(al1);
al.add(al2);
System.out.println("original arraylist: " + al.toString());
System.out.println("final arraylist: " + revertArrayList(al).toString());
}
结果截图: