- class StringTest3{
- public static void main(String[] args){
- String s1="abewmfhelloghesa";
- String s2="jnmdhellopq";
- Sop(getMaxSubString(s1,s2));
-
- }
- public static void Sop(Object obj){
- System.out.println(obj);
- }
- public static String getMaxSubString(String s1,String s2){
- for(int x=0;x<s2.length();x++)
- {
- for(int y=0,z=s2.length()-x;z!=s2.length()+1;y++,z++)
- {
- String temp= s2.substring(y,z);
- //Sop(temp);
- if(s1.contains(temp))
- return temp;
- }
- }
- return "";
- }
- }
复制代码 有如上代码:在getMaxSubString方法的内层循环中z的值是随着外层循环x的值的增加而递减,为什么还要z++?为什么要用z!=s2.length()+1来控制循环结束? |