aspose file tools*
The moose likes Beginning Java and the fly likes Trouble understanding Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble understanding Vectors" Watch "Trouble understanding Vectors" New topic
Author

Trouble understanding Vectors

Jason Pruitt
Greenhorn

Joined: Oct 23, 2005
Posts: 2
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.

// Begin class
public class NameList
{
Vector myVector = {"John", "Cornelius", "Jason", "Kym", "Luis", "Robin"};

Vector myVector = new Vector();
myVector.myList();

public static void main(String[] args)
{
}

private void myList()
{

for (Enumeration e = myVector.elements(); e.hasMoreElements()
{
String myString = (String) e.nextElement();
System.out.println(myString);
}

}
}
Eric McIntyre
Greenhorn

Joined: Mar 03, 2005
Posts: 26
Originally posted by Jason Pruitt:

// Begin class
public class NameList
{
Vector myVector = {"John", "Cornelius", "Jason", "Kym", "Luis", "Robin"};

Vector myVector = new Vector();
myVector.myList();

public static void main(String[] args)
{
}

private void myList()
{

for (Enumeration e = myVector.elements(); e.hasMoreElements()
{
String myString = (String) e.nextElement();
System.out.println(myString);
}

}
}

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
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
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

Layne


Java API Documentation
The Java Tutorial
Rick Portugal
Ranch Hand

Joined: Dec 17, 2002
Posts: 243
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.


IBM 286, SCJP, SCWCD, EIEIO
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

My 2 cents to your education...

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.

Example:

String x[] = new String[3];
x[0]="Doom III"
x[1]="World of Warcraft"
x[2]="Final Fantasy X"

2) ArrayList: An object that contains a list of objects of unknown type. The size of the array list can be modified.

ArrayList x = new ArrayList();
x.add("Doom III");
x.add("World of Warcraft");
x.add("Final Fantasy X");

3) Vector: Least used, it was replaced by ArrayList. Identical to ArrayList except that it is thread safe, a condition most people don't use.

Example is the same as ArrayList.

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.

Hope this helps.


My Blog: Down Home Country Coding with Scott Selikoff
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

3) Vector: Least used, it was replaced by ArrayList. Identical to ArrayList except that it is thread safe, a condition most people don't use.


..and there's a method that you can use to make ArrayLists threadsafe, anyway, so there's not really much call for Vectors anymore.

Still, Vectors are taught, and if the professor says, "use Vectors" then you should use Vectors.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
david lightman
Ranch Hand

Joined: Nov 03, 2004
Posts: 82
maybe this will help..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble understanding Vectors