Home > Java > javaTutorial > body text

Java Example - Output Array Elements

黄舟
Release: 2017-02-16 10:06:58
Original
1689 people have browsed it

The following example demonstrates how to output arrays of different types (integer, double and character) by overloading the printArray method of the MainClass class:

/*
 author by w3cschool.cc
 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);
   }}
Copy after login

The output result of the above code is:

输出整型数组:
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
Copy after login

The above is a Java example - outputting the contents of array elements. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template