Array Initialization

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Array Initialization

This Java example program shows how to initialize an array.

[edit] Java Source Code

  • Package: example.array
  • File: TestArray.java
  1. package example.array;
  2.  
  3. /*
  4.  * This example creates a four element array with 1, 2, 3, 4 as the values. The length of the array,
  5.  * and the element at index 2, are printed out.
  6.  */
  7. public class TestArray {
  8.     public static void main(String args[]) {
  9. 	int[] data = { 1, 2, 3, 4 };
  10.  
  11. 	System.out.println("The length of the array is " + data.length);
  12. 	System.out.println("The element at index 2 is " + data[2]);
  13.     }
  14. }

[edit] What Result You Can Get

Run the program, you will get:

The length of the array is 4
The element at index 2 is 3

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools