1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/* Java Reverse String Array Example This Java Reverse String Array example shows how to find sort an array of String in Java using Arrays and Collections classes. */ import java.util.Collections; import java.util.List; import java.util.Arrays; public class ReverseStringArrayExample { public static void main(String args[]){ //String array String[] strDays = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday"}; /* * There are basically two methods, one is to use temporary array and * manually loop through the elements of an Array and swap them or to use * Arrays and Collections classes. * * This example uses the second approach i.e. without temp variable. * */ //first create a list from String array List<String> list = Arrays.asList(strDays); //next, reverse the list using Collections.reverse method Collections.reverse(list); //next, convert the list back to String array strDays = (String[]) list.toArray(); System.out.println("String array reversed"); //print the reversed String array for(int i=0; i < strDays.length; i++){ System.out.println(strDays[i]); } } } /* Output of above given Java Reverse String Array example would be String array reversed Wednesday Tuesday Monday Sunday */ |
//The other way of reversing the string array is shown here
public void reverseStringArray2(){
String[] strreverse= {“abc”,”def”,”fff”,”ert”};
System.out.println(“Array values are “+Arrays.toString(strreverse));
for(int i=0; i<strreverse.length 2;=”” i++){=”” string=”” temp=”strreverse[i];” strreverse[i]=”strreverse[strreverse.length-i-1];” strreverse[strreverse.length-i-1]=”temp;” }=”” system.out.println(“reversed=”” array=”” values=”” are=”” “+arrays.tostring(strreverse));=”” }=””>
split the character from string
i want progarm reverse morethen two strings at a time.please any one help me.
how to get String reverse as WednessDay, TuesDay, Sunday, Monday
import java.util.*;
// code by Anand B.R.(SSE)
// code easily understood… Working fine…
public class Test_ReversingAnArray2{
public static void main(String args[]){
int a[]={1,5,78,23,2,99,10,77,9,90,22};
System.out.println(“the given array is “);
for(int i=0;i<a.length;i++){ system.out.print(a[i]+”\t”);=”” }=”” system.out.println();=”” int=”” temp;=”” temp=”” variable=”” for=”” swapping=”” between=”” 1st=”” n=”” last=”” index=”” elements=”” int=”” i;=”” for=”” getting=”” the=”” 1st=”” index=”” int=”” j=”a.length-1;” for=”” getting=”” the=”” last=”” index=”” for(int=”” i=”0;i<(a.length)/2;i++){” iterating=”” for=”” half=”” the=”” no=”” of=”” elements=”” temp=”a[i];” a[i]=”a[j];” a[j]=”temp;” j–;=”” decrementing=”” from=”” last=”” to=”” first=”” }=”” system.out.println(“the=”” new=”” again=”” reversed=”” array=”” is=”” “);=”” for(int=”” i=”0;i<a.length;i++){” system.out.print(a[i]+”\t”);=”” }=”” }=”” }=””>
input is like this string s=” my name is lokesh “
and out put String s=”my eman is hsekol”