Trouble understanding Vectors

I am having trouble understanding how to setup Vectors. I am a student and have to give a short code on Vectors. I have read some examples and am having trouble getting my code to run. Here is my code so far, could some please show me where I am going wrong. The task of the assignment is only to get a Vector working. Simple right? I wish.

At the rist of sounding flippant (teaching a man to fish and all that), what is your compiler telling you? Two hints: (1) Vectors and arrays are two different things; and (2) What class does the method myList() belong to?

Jason Pruitt
Greenhorn

Joined: Oct 23, 2005
Posts: 2

posted Oct 23, 2005 03:44:00

0

Im sorry, I just really do not understand. I am really new to programming and didn't quite grasp arrays. I am taking an online course and am kinda lost with what is going on. Please help

As Eric says, the compiler errors give you a hint where the problems are. If you don't understand what they mean, you should copy and paste them here and someone will explain them.

Note that Vectors are different than arrays. Look at this line:

You are trying to assign an array to a Vector. This cannot work since they are two different things. Vector is a class and you create instances of it just like you do any other class. You create Vectors just like you do any other class. In fact, the next line says:

It looks to me like that your troubles are more than just understanding Vectors. This looks like a general problem with understanding Java syntax. In particular, you cannot declare two variables with the same name like the above two lines do. So which one do you want?

Well, that's all I am going to say for now. Please post your compiler errors so that we can explain what they mean. That way you will be able to solve your problems on your own when you see similar errors next time.

Vectors aren't used as much as they used to be. If you are just starting out, I wouldn't spend a lot of time learning about Vectors. ArrayList is more useful. I would start there if you have the choice.

The structure your basically learning is list structures (for his sake, lets ignore Sets and Maps for now) since Lists are probably the most commonly used data structure especially for beginners.

Lists are exactly what they sound like, a set of elements indexed by a number, like having a clipboard and listed your 5 favorite video games in no particular order (lets also ignore ordering for this post).

By convention, the first element in the list is number 0. You could read much history about why this is, suffice it to say just accept that a list of size 3 has elements indexed by: 0, 1, and 2.

Finally, there are three unique and completely distinct types of commonly used lists. Be familiar with each of the three and more importantly how they differ:

1) Primitive Arrays (oftened referred to as typed arrays since they have a specific type): An object that contains a list of objects of a possibly specific type (you can create a typed array of Objects if you wanted). The size of the array cannot be changed. In order to change the size, you must create a new one and copy all the elements over one at a time.

Notice the differences, for the first, you had to define the size when you created the array. If you try x[3]="anything" it will fail since there are at most 3. For the Arraylist, you can add as many elements as you want.