Java迭代与递归-笔记

1.迭代方式实现:统计文本中相同字符的个数

 1 package test;
 2 
 3 public class hello {
 4         public int countA(String input) {
 5         // TODO Auto-generated method stub
 6         if (input == null || input.length()==0){             //首先如果输入为空,或者长度为0,那么结束程序
 7             return 0;
 8         }
 9         int count =0;                           //初始计数为0
10         for (int i=0;i<input.length();i++){                          //迭代,也就是每一个都来一遍,如果等于该字符,那么计数器自增
11             if (input.substring(i,i+1).equals("A")){
12                 count ++;
13             }
14         }
15         return count;                                         //最后返回计数器
16     }
17         public static void main(String [] args)
18         {
19             
20             System.out.println(new hello().countA("AAA rating"));       //new的使用         类.方法(参数)
21         }
22  } 

2. 使用递归方式:统计文本中相同字符的个数

如果一个方法调用了其自身的话,我们称之为递归调用.

所有的递归方法都是可重入的                          递归方法在循环树结构以及避免丑陋的嵌套循环的情况下是非常好用的。

 1 package test;
 2 
 3 public class hello {
 4         public int countA(String input) {
 5         // TODO Auto-generated method stub
 6         if (input == null || input.length()==0){
 7             return 0;
 8         }
 9         int count =0;
10         if (input.substring(0,1).equals("A")){            //每一次如果满足字符为A,就count赋值为1
11             count=1;
12         }
13         return count+countA(input.substring(1));             //countA();在表达式中,实现迭代
                                           //substring(1) 可以删除首字符,并将剩下的赋值给input。
14 } 15 public static void main(String [] args) 16 { 17 18 System.out.println(new hello().countA("AAAA rating")); 19 } 20 }

3. substring()的用法:

方法:

public String substring(int beginIndex, int endIndex)
第一个int为开始的索引,对应String数字中的开始位置,
第二个是截止的索引位置,对应String中的结束位置
1、取得的字符串长度为:endIndex - beginIndex;
2、从beginIndex开始取,到endIndex结束,从0开始数,其中不包括endIndex位置的字符

取长度大于等于3的字符串a的后三个子字符串,只需                  a.subString(a.length()-3, a.length());

 1 package test;
 2 public class hello {        
 3         public static void main(String [] args)
 4         {            
 5         String str="cisco";             //截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str
 6         str=str.substring(1);    
 7         System.out.println(str);
 8         
 9         }        
10  } 
输出:isco

4. 理解递归:

  • 可重入方法(re-entrant method)是可以安全进入的方法,即使同一个方法正在被执行,深入到同一个线程的调用栈里面也不会影响此次执行的安全性。一个非可重入方法则不是可以安全进入的。
  • 在尾递归中,最后要做的是递归,加法运算在之前就已经完成了。一轮递归调用完毕后就没有其他事情了(除了加法运算),因此调用时生成的信息也就没什么用了。这些无用信息可以丢弃,然后用一组新的参数来调用一次递归方法来产生一个新的结果。这也就是说,栈调用减少带来了内存消耗减少并且程序的性能更好。
     1 package test;
     2 public class hello {        
     3     public int countA(String input){                              //countA(String input);这个方法,如果输入为空,结束程序
     4             if (input ==null ||input.length()==0){
     5                 return 0;
     6             }
     7             return countA(input,0);                  //否则调用cuntA(input ,0); 这里的0是作为count的初始值
     8         
     9     }
    10     public int countA(String input,int count){
    11         if (input.length()==0){
    12             return count;                                 //如果长度为0那么,则返回计数0,直到最后长度为0了,返回count。
    13         }
    14         if (input.substring(0,1).equals("A")){
    15             count =count+1;
    16         }
    17         return countA(input.substring(1),count);                //尾递归
    18     }
    19     public static void main(String [] args)
    20         {                    
    21         System.out.println(new hello().countA("AAA rating"));
    22         }        
    23  } 
Coding
原文地址:https://www.cnblogs.com/ccie-leon-43093/p/5373386.html