pass an array as a function argument

M

Matt

when we pass an array as a function argument, we do this:

printArray(new int[] {4,5,6}); //good

OR

int [] a = {4,5,6};
printArray(a); //good

However, if we declare an array of integers like this, it will yield compile error:
int [] a = new int {1,2,3}; //compile error

what's the rationales? please advise. thanks!!
 
M

Murray

Matt said:
when we pass an array as a function argument, we do this:

printArray(new int[] {4,5,6}); //good

OR

int [] a = {4,5,6};
printArray(a); //good

However, if we declare an array of integers like this, it will yield compile error:
int [] a = new int {1,2,3}; //compile error

I think you mean

int[] a = new int[] {1,2,3}; // you forgot the second []
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top