java如何使用递归反转字符
作者:WeekendFlower
时间:2023-05-03
浏览:0
使用递归packagenet.javaguides.corejava.string;/****@authoryisu**/publicclassUsingRecursion{staticinti=0;//RecursivefunctiontoreverseastringinJavausingstaticvariableprivatestaticvoidreverse(char[]str,intk){//ifwehavereachedtheendofthestringif(k==str.length)ret
本文目录
使用递归
package net.javaguides.corejava.string;
/**
*
* @author yisu
*
*/
public class UsingRecursion {
static int i = 0;
// Recursive function to reverse a string in Java using static variable
private static void reverse(char[] str, int k) {
// if we have reached the end of the string
if (k == str.length)
return;
// recurse for next character
reverse(str, k + 1);
if (i <= k) {
char temp = str[k];
str[k] = str[i];
str[i++] = temp;
}
}
public static String reverse(String str) {
// base case: if string is null or empty
if (str == null || str.equals(""))
return str;
// convert string into a character array
char[] A = str.toCharArray();
// reverse character array
reverse(A, 0);
// convert character array into the string
return String.copyValueOf(A);
}
public static void main(String[] args) {
String str = "Java Guides";
// string is immutable
str = reverse(str);
System.out.println("Reverse of the given string is : " + str);
}
}输出:
Reverse of the given string is : sediuG avaJ
作者最新文章
SQL条件逻辑转换:CASE WHEN语句在数据分类中的应用
2026-10-13 16:41
漫蛙官网入口最新版本下载
2026-10-13 13:19
漫蛙动漫最新版本app下载安装
2026-10-13 13:18
漫蛙APP下载安装免费版
2026-10-13 13:18
漫蛙漫画登录入口免费漫画在线阅读苹果版
2026-10-13 13:17
上一篇:
如何从 iPhone 中恢复数据?
下一篇:
Python怎么获取视频文件的大小和时长
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多









































