Provide Java code to create and initialize an array of integ
Provide Java code to create and initialize an array of integers (Java primitive int), floats or any other Java primitive type of your choice.
You pick the array name and length.
Demonstrate how you would determine the length of your array. Show this through a code example you create where you use the length member.
Solution
There are two ways to initialize array both ways are given below:
Method 1:
Code:
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int a[]=new int[5]; //Array declaration
//Initialize array with values
a[0]=16;
a[1]=26;
a[2]=36;
a[3]=46;
a[4]=56;
//Find the length of array
int len = a.length;
//Print the length
System.out.println(\"The length of array is :\" + len);
}
}
Output:
Method 2:
Code:
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
//Array declaration and initialization
int a[]= {2,4,5,6,7,8};
a[4]=56;
//Find the length of array
int len = a.length;
//Print the length
System.out.println(\"The length of array is :\" + len);
}
}
Output:

