问一下for嵌套里的return temp应该只能跳出里层for循环,为什么最终运行结果是对的?
还有return "";的作用是什么?为什么它不会把return temp的结果覆盖掉,还有它为什么要放在for循环体外?
class Test4
{
public static void main(String[] args)
{
String s1 ="abcwerthelloyuiodef";
String s2 ="cvhellobnm";
sop(getMaxsSubString(s2,s1));
}
public static String getMaxsSubString(String s1,String s2)
{
String max="",min="";
max = (s1.length()>min.length())?s1:s2;
min = (max==s1)?s2:s1;
for (int x = 0;x<min.length() ;x++ )
{
for (int y =0,z=min.length()-x;z!=min.length()+1 ;y++,z++ )
{
String temp = min.substring(y,z);
if (max.contains(temp))
return temp;
}
}
return "";
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
|
|