Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Output array elements

Java Examples - Output Array Elements

Java Examples Java Examples

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

MainClass.java File

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

(

"

Output integer array:

"

)

;

printArray

(

integerArray

)

;

System

.

out

.

println

(

"

\n

Output double array:

"

)

;

printArray

(

doubleArray

)

;

System

.

out

.

println

(

"

\n

Output character array:

"

)

;

printArray

(

characterArray

)

;

}

}

The output of the above code is:


Output integer array:
1 
2 
3 
4 
5 
6 

// Output double-precision array:
1.1 
2.2 
3.3 
4.4 
5.5 
6.6 
7.7 

Output character array:
H 
E 
L 
L 
O 

Java Examples Java Examples