Originally posted by sheng zhong: int N =12; int near  = new int[N];
Why should we use "= new int[N]" to define a array, what is the "new int[N]" means?
Why not define a array like this: int near ;
As Pablo mentioned, a Java array is an object. Its type includes the type of references (or primitive values) it will hold, along with its "dimensions" (for example, a one-dimensional array, two-dimensional array, three-dimensional array, etc.).
The array type does not include its size, which is required for the array's instantiation. (In this respect, you might think of the size as a parameter passed to the array's constructor.) So -- unlike dimensions -- the size of an array never appears in the declaration (left of the = assignment).
So in your example of int near  = new int[N]; the declaration on the left-hand-side is a one-dimensional array of ints (the type). On the right-hand-side is the creation of a new object, which is an int array with a size of N.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Nov 23, 2006
Ok, I get it. I can't define a array in the way of C or c++. Since it's an object, so we must assign a space for it. So it must have =new int[N], right?