Example of a java loop array element

Source: Internet
Author: User


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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.