java - Trouble creating a method to reverse an arraylist -
i'm having problem trying reverse arraylist using loop. did, doesn't reverse array:
import java.util.*; class test { public static void main(string[] args) { arraylist<integer> list = new arraylist<integer>(); list.add(3); list.add(4); list.add(5); reverse(list); } public static void reverse(arraylist<integer> list){ integer [] reverse = new integer[list.size()]; (int i=0; i<list.size(); i++){ reverse[i] = list.get(0); system.out.println (reverse[i]); } } }
this output expected:
5 4 3
while i'm getting:
$ java test 3 3 3
public static void reverse(arraylist<integer> list){ integer [] reverse = new integer[list.size()]; (int i=list.size()-1; i>=0; i--){ reverse[i]= list.get(i); system.out.println (reverse[i]); } }
Comments
Post a Comment