karma karma - 3 months ago 8
Java Question

What is the variable 'i' mean after the array declaration

I came across a Java program to merge sort some numbers. Here is a snippet of the code which I am a bit confused with.

int n;
Scanner in=new Scanner(System.in);
n=in.nextInt();
int A []=new int[n],i;
for(i=0;i<n;++i)


In the above code the line
int A []=new int[n],i;
What does this
i
stand for. Is it a declaration of an int variable called
i
? Or does it have another meaning. Your advice will be appreciated.

Answer

It is declaration of int type variable. But it is more better to declare that int i in for like this

for(int i = 0; i < n; ++i)

Also more whitespaces increase code readability

You can declare variables like this TYPE var1, var2, var3, ..., varN;

And all variables will have same type

Comments