2014年5月14日星期三

How to define a method java available for calls to achieve string in reverse output

How to define a method java available for calls to achieve string in reverse output , such as: the input string "abcdef", after calling the method reverse output "fedcba"
------ Solution - -------------------------------------------

public void outPut(String strSrc)
{
for(int i=1;i<=strSrc.length();i++)
{
System.out.print(strSrc.charAt(strSrc.length()-i));
}
return ;
}
< br> ------ Solution ----------------------------------------- ---
own definition , then , it can be
public static String StringReverse (String string) {
char [] charctor = string.toCharArray () ;/ / convert an array of characters
char [] char1 = new char [string.length ()] array ;/ / descending store
for (int i = 0; i char1 [i] = charctor [string.length ()-i-1];
}
String string2 = new String (char1);
return string2;

}
OK the
------ Solution ---------------------------------- ----------
1 create a StringBuilder.;
2 flashbacks to traverse the original character string added to the StringBuilder . ;
3. return StringBuilder.toString () the result value ( string ) .
------ Solution ---------------------------------------- ----

positive solution, or they use a char array , reverse output
------ For reference only ----------------- ----------------------
StringBuilder sb = new StringBuilder ("abc");
sb.reverse ();
stringbuilder already done for you .
------ For reference only -------------------------------------- -


I read the source code, if the interviewer to your own definition?
------ For reference only -------------------------------------- -
  
  
I read the source code, if the interviewer to your own definition?  
turn a string array , reverse output
------ For reference only ----------------- ----------------------
already have their own methods to traverse or use reverse.
------ For reference only -------------------------------------- -
  
  
I read the source code, if the interviewer to your own definition?   find the value of the offset address field , then descending to write directly to memory . interviewer suddenly sb the
--- --- For reference only ---------------------------------------
descending characters Yeah string array

没有评论:

发表评论