Example:
Public class Test {
Public static void main (String args []) {
Int [] numbers = {10, 20, 30, 40, 50 };
For (int x: numbers ){
System. out. print (x );
System. out. print (",");
}
System. out. print ("
");
String [] names = {"James", "Larry", "Tom", "Lacy "};
For (String name: names ){
System. out. print (name );
System. out. print (",");
}
}
}
This produces the following results:
10, 20, 30, 40, 50,
James, Larry, Tom, Lacy,
Example
The following example demonstrates how to output arrays of different types (integer, double precision, and character type) by reloading 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 ("output integer array :");
PrintArray (integerArray );
System. out. println ("\ n output dual-Precision Array :");
PrintArray (doubleArray );
System. out. println ("\ n output struct type array :");
PrintArray (characterArray );
}
}
The output result of the above code is:
Output integer array:
1
2
3
4
5
6
Output Dual-precision array:
1.1
2.2
3.3
4.4
5.5
6.6
7.7
Output struct array:
H
E
L
L
O
Well, there are still many methods for traversing loops. We can look for them on this site. For example, foreach is very useful. I haven't introduced it here.