集册 Java实例教程 使用重载方法打印数组元素。

使用重载方法打印数组元素。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

385
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用重载方法打印数组元素。

public class Main /* 来 自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m*/

{

   public static void main(String[] args) 

   {

      // create arrays of Integer, Double and Character

      Integer[] integerArray = {1, 2, 3, 4, 5, 6};

      Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};

      Character[] characterArray = {'H', 'E', 'L', 'L', 'O'};


      System.out.printf("Array integerArray contains:%n");

      printArray(integerArray); // pass an Integer array

      System.out.printf("%nArray doubleArray contains:%n");

      printArray(doubleArray); // pass a Double array

      System.out.printf("%nArray characterArray contains:%n");

      printArray(characterArray); // pass a Character array
      /*
      来 自*
       N o w J a v a . c o m
      */

   } 


   // method printArray to print Integer array

   public static void printArray(Integer[] inputArray)

   {

      // display array elements

      for (Integer element : inputArray)

         System.out.printf("%s ", element);


      System.out.println();

   }


   // method printArray to print Double array

   public static void printArray(Double[] inputArray)

   {

      // display array elements

      for (Double element : inputArray)

         System.out.printf("%s ", element);


      System.out.println();

   }


   
展开阅读全文