Saturday, February 23, 2008

Java Array or Vector: What's the Difference?

Arrays and Vectors in Java are quite similar. They both represent a collection of similar items. For example, a good usage of an array would be a variable called states ( as in the United States of America).

String[] states = new String[50];

This is great for a collection of objects or primitive data types with an unchanging number of elements. We know that there are 50 states (I suppose that could change, but in general it is a fixed list). What do we do when we're not sure how many elements will be in our collection?

Use a Vector!

If you want to build a shopping list, you probably won't know how many items will be in the list, so a Vector would be great for this type of variable.

Vector myShoppingList= new Vector();
myShoppingList.addElement("bananas");
myShoppingList.addElement("milk");
myShoppingList.addElement("coffee");

I can keep adding elements to myShoppingList without knowing ahead of time how many items there will be. Thanks to LET Exam Results September 2008 and Nintendo DS Games Free Download.


The comptia a+ certification is not possible until basics like ccna is covered. This is important for mcsa certification as well as itil certification.

No comments:

Favorite Feeds