java - How to reverse an ArrayList without the reverse method? -
hello trying reverse arraylist without reverse method. wanted make work without method. can't seem right. have far:
for (int x = nums.size()-1; x>=0; x--) { for(int z =0; z<nums.size();z++) { nums.set(z, x); } }
this output: run: 0 1 2 3
1 1 1 1 build successful (total time: 0 seconds)
you can ascend bottom & simultaneously descend top (size() - 1
) swapping elements, , stop when meet in middle.
int = 0; int j = nums.size()-1; while (i < j) { int temp = nums.get(i); nums.set( i, nums.get(j)); nums.set( j, temp); i++; j--; }
Comments
Post a Comment