Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i found this code that confused me thought something was wrong with the writing of the array but tried it in java and worked

What I have tried:

tried searching the code int[] v=new int[N],i; but found nothing
Posted
Updated 18-Oct-21 20:08pm

The comma (',') is a separator in Java. hence the line
Java
int [] v = new int[2], i;
declares and initializes v as an array of int and declares i as an array of int as well (note i is left uninitialized).

Try
Java
public class Foo
{ 
  public static void main( String args[] )
  {
    int [] v = new int[2], i;

    i = new int[3];

    v[1] = 7;
    i[2] = 42;

    System.out.println(v[1]);
    System.out.println(i[2]);
  }
}
 
Share this answer
 
Comments
Maciej Los 19-Oct-21 2:07am    
5ed!
CPallini 19-Oct-21 2:20am    
Thank you, Maciej!
SilentH11 19-Oct-21 2:27am    
thanks for the help!!🤗
CPallini 19-Oct-21 2:34am    
You are welcome.
You don't have to: the comma allows you to declare a second or third variable of the same type if you wish:
Java
int[] v=new int[N],i;
...
String name = v.getClass().getSimpleName();
System.out.println(name);
name = i.getClass().getSimpleName();
System.out.println(name);
Will tell you that both v and i are declared as int[]
 
Share this answer
 
v2
Comments
CPallini 19-Oct-21 2:22am    
+5.
Nice.
Maciej Los 19-Oct-21 2:33am    
Also 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900