Java 如何在不使用循环的情况下递归地从给定字符串中删除char数组中的所有字符?
我正在练习Java教程,我正在尝试从给定字符串中删除char数组中给定的所有字符(例如,数组包含“b”、“m”、“w”。目标字符串是“big workshop”,输出:“ig orkshop”)。但是我不能使用循环,我应该递归地使用它。我在没有递归的情况下管理了它,但没有使用递归。 这是我的非递归代码:Java 如何在不使用循环的情况下递归地从给定字符串中删除char数组中的所有字符?,java,recursion,Java,Recursion,我正在练习Java教程,我正在尝试从给定字符串中删除char数组中给定的所有字符(例如,数组包含“b”、“m”、“w”。目标字符串是“big workshop”,输出:“ig orkshop”)。但是我不能使用循环,我应该递归地使用它。我在没有递归的情况下管理了它,但没有使用递归。 这是我的非递归代码: char[] testChars={'E', 'i', 'n'}; String b = new String(testChars); ... public static String r
char[] testChars={'E', 'i', 'n'};
String b = new String(testChars);
...
public static String removeChars(String text)
{
return text.replaceAll("[" + b + "]", "");
}
可以通过以下方式替换for循环:
public void replaceFor(int i , Predicate<Integer> p , Consumer<Integer> c , Function<Integer , Integer> f)
{
//check whether the termination-condition is true
if(!p.test(i))
return;
//this consumer does what would be in the for-loop
c.accept(i);
//continue with the next value for i
replaceFor(f.apply(i) , p , c , f);
}
public void replaceFor(int i,谓词p,消费者c,函数f)
{
//检查终止条件是否为真
如果(!p.test(i))
返回;
//此消费者执行for循环中的操作
c、 接受(i);
//继续输入i的下一个值
替换(f.apply(i)、p、c、f);
}
打印从0到10的所有数字的基本for循环替换如下所示:
replaceFor(0 , i -> i <= 10 , i -> System.out.println(i) , i -> ++i);
replaceFor(0,i->i System.out.println(i),i->++i);
您可以通过以下方式替换for循环:
public void replaceFor(int i , Predicate<Integer> p , Consumer<Integer> c , Function<Integer , Integer> f)
{
//check whether the termination-condition is true
if(!p.test(i))
return;
//this consumer does what would be in the for-loop
c.accept(i);
//continue with the next value for i
replaceFor(f.apply(i) , p , c , f);
}
public void replaceFor(int i,谓词p,消费者c,函数f)
{
//检查终止条件是否为真
如果(!p.test(i))
返回;
//此消费者执行for循环中的操作
c、 接受(i);
//继续输入i的下一个值
替换(f.apply(i)、p、c、f);
}
打印从0到10的所有数字的基本for循环替换如下所示:
replaceFor(0 , i -> i <= 10 , i -> System.out.println(i) , i -> ++i);
replaceFor(0,i->i System.out.println(i),i->++i);
您可以通过以下方式替换for循环:
public void replaceFor(int i , Predicate<Integer> p , Consumer<Integer> c , Function<Integer , Integer> f)
{
//check whether the termination-condition is true
if(!p.test(i))
return;
//this consumer does what would be in the for-loop
c.accept(i);
//continue with the next value for i
replaceFor(f.apply(i) , p , c , f);
}
public void replaceFor(int i,谓词p,消费者c,函数f)
{
//检查终止条件是否为真
如果(!p.test(i))
返回;
//此消费者执行for循环中的操作
c、 接受(i);
//继续输入i的下一个值
替换(f.apply(i)、p、c、f);
}
打印从0到10的所有数字的基本for循环替换如下所示:
replaceFor(0 , i -> i <= 10 , i -> System.out.println(i) , i -> ++i);
replaceFor(0,i->i System.out.println(i),i->++i);
您可以通过以下方式替换for循环:
public void replaceFor(int i , Predicate<Integer> p , Consumer<Integer> c , Function<Integer , Integer> f)
{
//check whether the termination-condition is true
if(!p.test(i))
return;
//this consumer does what would be in the for-loop
c.accept(i);
//continue with the next value for i
replaceFor(f.apply(i) , p , c , f);
}
public void replaceFor(int i,谓词p,消费者c,函数f)
{
//检查终止条件是否为真
如果(!p.test(i))
返回;
//此消费者执行for循环中的操作
c、 接受(i);
//继续输入i的下一个值
替换(f.apply(i)、p、c、f);
}
打印从0到10的所有数字的基本for循环替换如下所示:
replaceFor(0 , i -> i <= 10 , i -> System.out.println(i) , i -> ++i);
replaceFor(0,i->i System.out.println(i),i->++i);
试试这个
public class Example
{
public static void main(String[] agrs) {
String input = "big workshop";
char[] charToRemove = {'b', 'm', 'w'};
String charsToRemove = new String(charToRemove);
StringBuilder sb = new StringBuilder();
Example ex = new Example();
ex.removeChar(input, 0, charsToRemove, sb);
System.out.println(sb);
}
public void removeChar(String input, int index, String charToRemove, StringBuilder target) {
if(input.length() == index) {
return;
}
char c = input.charAt(index);
if(charToRemove.indexOf(c) == -1) {
target.append(c);
}
removeChar(input, index + 1, charToRemove, target);
}
}
试试这个
public class Example
{
public static void main(String[] agrs) {
String input = "big workshop";
char[] charToRemove = {'b', 'm', 'w'};
String charsToRemove = new String(charToRemove);
StringBuilder sb = new StringBuilder();
Example ex = new Example();
ex.removeChar(input, 0, charsToRemove, sb);
System.out.println(sb);
}
public void removeChar(String input, int index, String charToRemove, StringBuilder target) {
if(input.length() == index) {
return;
}
char c = input.charAt(index);
if(charToRemove.indexOf(c) == -1) {
target.append(c);
}
removeChar(input, index + 1, charToRemove, target);
}
}
试试这个
public class Example
{
public static void main(String[] agrs) {
String input = "big workshop";
char[] charToRemove = {'b', 'm', 'w'};
String charsToRemove = new String(charToRemove);
StringBuilder sb = new StringBuilder();
Example ex = new Example();
ex.removeChar(input, 0, charsToRemove, sb);
System.out.println(sb);
}
public void removeChar(String input, int index, String charToRemove, StringBuilder target) {
if(input.length() == index) {
return;
}
char c = input.charAt(index);
if(charToRemove.indexOf(c) == -1) {
target.append(c);
}
removeChar(input, index + 1, charToRemove, target);
}
}
试试这个
public class Example
{
public static void main(String[] agrs) {
String input = "big workshop";
char[] charToRemove = {'b', 'm', 'w'};
String charsToRemove = new String(charToRemove);
StringBuilder sb = new StringBuilder();
Example ex = new Example();
ex.removeChar(input, 0, charsToRemove, sb);
System.out.println(sb);
}
public void removeChar(String input, int index, String charToRemove, StringBuilder target) {
if(input.length() == index) {
return;
}
char c = input.charAt(index);
if(charToRemove.indexOf(c) == -1) {
target.append(c);
}
removeChar(input, index + 1, charToRemove, target);
}
}
尝试:
尝试:
尝试:
尝试:
当尝试使用递归时,有两种情况需要记住,要么是基本情况,要么是朝着基本情况迈出了一步 例如:基本大小写可以是字符串的结尾。每个递归级别有两种可能 1) 您位于字符串的末尾:返回一个空字符串以用作建筑基础 2) 您不在字符串的末尾:您可以检查第一个字符,并将字符串的其余部分传递给递归调用 请参见下面的示例。这不是经过测试的代码,但应该为您指明正确的方向
public String recursiveRemove (String[] arr, String str){
// first check if at the base case
if (str.length() == 0) {
return "";
}
// else handle character, and reduce to approach base case
String character = str.substring(0,1);
// contains is not a method but just to show the logic being used here
if (arr.contains(character)){
//replace character with empty sting to remove it from the result
character = "";
}
// return the character (or empty string) with the result of the
// recursive call appended onto the end
return character + recursiveRemove(arr, str.substring(1));
}
当尝试使用递归时,有两种情况需要记住,要么是基本情况,要么是朝着基本情况迈出了一步 例如:基本大小写可以是字符串的结尾。每个递归级别有两种可能 1) 您位于字符串的末尾:返回一个空字符串以用作建筑基础 2) 您不在字符串的末尾:您可以检查第一个字符,并将字符串的其余部分传递给递归调用 请参见下面的示例。这不是经过测试的代码,但应该为您指明正确的方向
public String recursiveRemove (String[] arr, String str){
// first check if at the base case
if (str.length() == 0) {
return "";
}
// else handle character, and reduce to approach base case
String character = str.substring(0,1);
// contains is not a method but just to show the logic being used here
if (arr.contains(character)){
//replace character with empty sting to remove it from the result
character = "";
}
// return the character (or empty string) with the result of the
// recursive call appended onto the end
return character + recursiveRemove(arr, str.substring(1));
}
当尝试使用递归时,有两种情况需要记住,要么是基本情况,要么是朝着基本情况迈出了一步 例如:基本大小写可以是字符串的结尾。每个递归级别有两种可能 1) 您位于字符串的末尾:返回一个空字符串以用作建筑基础 2) 您不在字符串的末尾:您可以检查第一个字符,并将字符串的其余部分传递给递归调用 请参见下面的示例。这不是经过测试的代码,但应该为您指明正确的方向
public String recursiveRemove (String[] arr, String str){
// first check if at the base case
if (str.length() == 0) {
return "";
}
// else handle character, and reduce to approach base case
String character = str.substring(0,1);
// contains is not a method but just to show the logic being used here
if (arr.contains(character)){
//replace character with empty sting to remove it from the result
character = "";
}
// return the character (or empty string) with the result of the
// recursive call appended onto the end
return character + recursiveRemove(arr, str.substring(1));
}
当尝试使用递归时,有两种情况需要记住,要么是基本情况,要么是朝着基本情况迈出了一步 例如:基本大小写可以是字符串的结尾。每个递归级别有两种可能 1) 您位于字符串的末尾:返回一个空字符串以用作建筑基础 2) 您不在字符串的末尾:您可以检查第一个字符,并将字符串的其余部分传递给递归调用 请参见下面的示例。这不是经过测试的代码,但应该为您指明正确的方向
public String recursiveRemove (String[] arr, String str){
// first check if at the base case
if (str.length() == 0) {
return "";
}
// else handle character, and reduce to approach base case
String character = str.substring(0,1);
// contains is not a method but just to show the logic being used here
if (arr.contains(character)){
//replace character with empty sting to remove it from the result
character = "";
}
// return the character (or empty string) with the result of the
// recursive call appended onto the end
return character + recursiveRemove(arr, str.substring(1));
}
这里有一个解决方案
- 维护您的方法签名
- 不使用递归屏蔽迭代
- 教你分而治之
private final static char[] testChars = {'b', 'm', 'w'};
public static String removeChars(String text) {
switch (text.length()) {
case 0:
return "";
case 1:
char asChar = text.charAt(0);
for (char testChar : testChars) {
if (asChar == testChar) {
return "";
}
}
return text;
default:
int middle = text.length() / 2;
String firstHalf = text.substring(0, middle);
String lastHalf = text.substring(middle);
return removeChars(firstHalf) + removeChars(lastHalf);
}
}
public static void main(String... args) {
System.out.println(removeChars("big workshop"));
}
这里有一个解决方案
- 维护您的方法签名
- 不使用递归屏蔽迭代
- 教你分而治之
private final static char[] testChars = {'b', 'm', 'w'};
public static String removeChars(String text) {
switch (text.length()) {
case 0:
return "";
case 1:
char asChar = text.charAt(0);
for (char testChar : testChars) {
if (asChar == testChar) {
return "";
}
}
return text;
default:
int middle = text.length() / 2;
String firstHalf = text.substring(0, middle);
String lastHalf = text.substring(middle);
return removeChars(firstHalf) + removeChars(lastHalf);
}
}
public static void main(String... args) {
System.out.println(removeChars("big workshop"));
}
这里有一个解决方案
- 维护您的方法签名
- 不使用递归屏蔽迭代
- 教你分而治之
private final static char[] testChars = {'b', 'm', 'w'};
public static String removeChars(String text) {
switch (text.length()) {
case 0:
return "";
case 1:
char asChar = text.charAt(0);
for (char testChar : testChars) {
if (asChar == testChar) {
return "";
}
}
return text;
default:
int middle = text.length() / 2;
String firstHalf = text.substring(0, middle);
String lastHalf = text.substring(middle);
return removeChars(firstHalf) + removeChars(lastHalf);
}
}
public static void main(String... args) {
System.out.println(removeChars("big workshop"));
}
这里有一个解决方案
- 维护您的方法签名
- 不能掩盖它