Home  >  Forum  >  Core Java
Post New Query

printing the elements of array in java



Asked on March 10, 2015
I want to print the all elements of my array list but got these error. please help to fix them. Thanks 
Error:

My Code is:

public class MyArray {
public static void main(String[] args) {
int [] nums;
nums = new int[7];
nums[0] = 6;
nums[1] = 12;
nums[2] = 13;
nums[3] = 22;
nums[4] = 9;
nums[5] = 33;
nums[6] = 44;
for(int i = 0; i <= 6; i++){
System.out.println(nums);
}
}

}




Replied on March 10, 2015
for(int i = 0; i <= 6; i++)
{
    System.out.println(nums[i]);
}



Replied on March 10, 2015
You can also print it as 

for(int i = 0; i < nums.length; i++)
{
    System.out.println(nums[i]);
}




Replied on March 11, 2015
You have stored your all array element in nums variable and you want to print it, so almost you have done best to print the element using for loop.
for(int i = 0; i <= 6; i++)
When your loop is running then it will store all array element in variable i and your loop will print all element one bye one. Your print statement is like this...
System.out.println(nums[i]);


Write Answer







©2019 concretepage.com | Privacy Policy | Contact Us