如何在 Java 中使用方法重载打印不同类型的数组



问题描述

如何使用方法重载打印不同类型的数组?

解决方案

此示例展示了如何在一个循环中跳到特定标签,当出现 break 或者 continue 语句时。

public class NewClass {
   public static void main(String[] args) {
      String strSearch = "This is the string in which you have to search for a substring.";
      String substring = "substring";
      boolean found = false;
      int max = strSearch.length() - substring.length();
      testlbl: for (int i = 0; i <= max; i++) {
         int length = substring.length();
         int j = i;
         int k = 0;
         while (length-- != 0) {
            if(strSearch.charAt(j++) != substring.charAt(k++)){
               continue testlbl;
            }
         }
         found = true;
         break testlbl;
      }
      if (found) {
         System.out.println("Found the substring .");
      } else {
         System.out.println("did not find the substing in the string.");
      }
   }
}

结果

上述代码样例将产生以下结果。

Found the substring .
java_methods.htm
广告