jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Array vs Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array vs Vector" Watch "Array vs Vector" New topic
Author

Array vs Vector

Mandy Smith
Ranch Hand

Joined: Jun 26, 2001
Posts: 62
Hi,
I have a situation where user can input the name of the files through html form (1 or 2 file, max is 2 files).
Is it better to hold the file names in an array or in a vector?
The problem is if i store it in an array, as the size of the array is 2. The element at location 2 in the array is null, which is leading to nullpointerexception when user submits only 1 file name. So, i am using vector instead of array to avoid this exception.
Is this ok to do? If not, what's is the efficient way to do?
Appreciate your input.
Thanks,
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
sure , no problem... but maybe consider using ArrayList which is better than Vector.
Mandy Smith
Ranch Hand

Joined: Jun 26, 2001
Posts: 62
Thanks for your input!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mandy Smith:
The problem is if i store it in an array, as the size of the array is 2. The element at location 2 in the array is null, which is leading to nullpointerexception when user submits only 1 file name. So, i am using vector instead of array to avoid this exception.

Why don't you instantiate an array of the correct size (1 or 2 depending on the number of entered file names?
Can you post some code?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dean Jones
Greenhorn

Joined: Jun 18, 2002
Posts: 24
As such u should not have any problem in using the arrays if the no. of elements are fixed. u shuld remember that array indexes starts from zero instead of 1 , so if u have used a array with the limit two say Array a[2]; and latter u trying to access a[2] element this will trey to acces the third element and thus will generate e exeption ArrayOutOfBoundsEexception,but in ur case it is null pointer esception so might be ur not able to put the data in the array b4 using it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array vs Vector
 
Similar Threads
Result Set Sorting
Vector with bean:write tag
Using result sets, statements & connections
How to refresh a StringBuffer object for creating a HashMap object
Reading File