-3

I need to write a method that takes an arraylist of Strings and returns a new arraylist of strings where the elements are in reversed order. The original list needs to stay the same. any tips on how to handle this? any help would be great! thanks!

public static ArrayList<String> reverse(ArrayList<String> list) {
    //Note:  original 'list' must remain unchanged!!

    }

    //dummy return value
    return new ArrayList<String>();
}
kennycodes
  • 526
  • 3
  • 11
  • 19

1 Answers1

4

Copy the input List, and then use Collections.reverse(List). Something like

public static <T> List<T> reverse(List<T> list) {
    List<T> al = new ArrayList<>(list);
    Collections.reverse(al);
    return al;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249