• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Array vs Vector

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure , no problem... but maybe consider using ArrayList which is better than Vector.
 
Mandy Smith
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!