-1

I do have a array with million String objects..

ex:String values [] = new String[10000000] 

and I've to reverse all the elements and what is the best way of doing it to improve performance?

TP_JAVA
  • 1,002
  • 5
  • 23
  • 49
  • 4
    You can only *improve* performance if you have something to measure it against. Please post your current code. – Andy Turner Sep 23 '15 at 22:40
  • 3
    It'll be O(n) in any case. – raina77ow Sep 23 '15 at 22:40
  • 1
    Code a really bad way to do it. Then make it better ... and way you will have improved performance :-) – Stephen C Sep 23 '15 at 22:44
  • The obvious solution where swap elements starting from the ends and moving to the middle should be optimal, I think. No, I ain't gunna code it for you! – Stephen C Sep 23 '15 at 22:46
  • 1
    Probable duplicate of http://stackoverflow.com/q/9995432/869736 – Louis Wasserman Sep 23 '15 at 23:35
  • Creating another array and copying elements in reverse order may not be the optimal solution. You can create `reverse view` of your original array, without actually copying it. This view may be represented by `Iterable` that wraps your original array and creates iterators that traverse it in reverse order. – Aivean Sep 24 '15 at 00:55

1 Answers1

1

You are making you task harder than it needs to be by not using the Collections standard library. Instead of an array use a List then use the standard Collections.reverse() method.

List<String> list = Arrays.asList(values);
Collections.reverse(list);
bhspencer
  • 13,086
  • 5
  • 35
  • 44