Reputation: 11
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] v = new int[n];
}
}
Is there any problem in doing this?
Upvotes: 0
Views: 275
Reputation: 6078
The good thing using class like vector is that it encapsulate all the management you need to do with your array. So, as Tom Hawtin - tackline said, it would manage unexpected value but it will also manage resize, and it will provide you some usefull method to deal with your array, like .get witch will disallow you to get an item out of bound and many more method you can get there : http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html
btw, as a side note I would recommand you to use an ArrayList : http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
Upvotes: 0
Reputation: 147154
No. (Other than n
might be negative, or be huge causing a Denial-of-Service (DoS) condition.)
With arrays you can't change the size later. You have to create a new array, copy the contents and switch all references over.
Upvotes: 3
Reputation: 284786
No, but unlike Vector it is obviously not resizable. Note that in Java, all collections are allocated on the heap.
Upvotes: 0