reverse() in java code example
Example 1: reverse array in java
int length = array.length;
for(int i=0;i<length/2;i++) {
int swap = array[i];
array[i] = array[length-i-1];
array[length-i-1] = swap;
}
or
Collections.reverse(Arrays.asList(array));
Example 2: how to reverse a list in java
List<String> stringList = Arrays.asList("A", "B", "C");
Collections.reverse(stringList);
assertThat(stringList).containsExactly("C", "B", "A");
Example 3: how to reverse a string in java
public class ReverseString {
public static void main(String[] args) {
String s1 = "neelendra";
for(int i=s1.length()-1;i>=0;i--)
{
System.out.print(s1.charAt(i));
}
}
}
Example 4: java string reverse
String rev = new StringBuilder("Your String").reverse().toString();
Example 5: string reverse in java
String str = "Hello";
String reverse(String str){
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.reverse();
return sb.toString();
}
Example 6: reverse array java
for(int i=intArray.length-1;i>=0;i--)
System.out.print(intArray[i] + " ");