Java 实例 - 输出数组元素

Java 教程 · 2019-02-10 15:14:50

以下实例演示了如何通过重载 MainClass 类的 printArray 方法输出不同类型(整型, 双精度及字符型)的数组:

MainClass.java 文件

public class MainClass { public static void printArray(Integer[] inputArray) { for (Integer element : inputArray){ System.out.printf("%s ", element); System.out.println(); } } public static void printArray(Double[] inputArray) { for (Double element : inputArray){ System.out.printf("%s ", element); System.out.println(); } } public static void printArray(Character[] inputArray) { for (Character element : inputArray){ System.out.printf("%s ", element); System.out.println(); } } public static void main(String args[]) { 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.println("输出整型数组:"); printArray(integerArray); System.out.println("\n输出双精度型数组:"); printArray(doubleArray); System.out.println("\n输出字符型数组:"); printArray(characterArray); } }

以上代码运行输出结果为:

输出整型数组:
1 
2 
3 
4 
5 
6 

输出双精度型数组:
1.1 
2.2 
3.3 
4.4 
5.5 
6.6 
7.7 

输出字符型数组:
H 
E 
L 
L 
O 

点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html

查看所有标签

Introduction to the Design and Analysis of Algorithms

Introduction to the Design and Analysis of Algorithms

Anany Levitin / Addison Wesley / 2011-10-10 / USD 117.00

Based on a new classification of algorithm design techniques and a clear delineation of analysis methods, Introduction to the Design and Analysis of Algorithms presents the subject in a coherent a......一起来看看 《Introduction to the Design and Analysis of Algorithms》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具