The Artima Developer Community
Sponsored Link

Java Answers Forum
having troubles in vector

7 replies on 1 page. Most recent reply: Apr 11, 2002 1:19 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Dani

Posts: 1
Nickname: rio
Registered: Apr, 2002

having troubles in vector Posted: Apr 10, 2002 10:41 PM
Reply to this message Reply
Advertisement
i'm having trouble putting a word for each node in vector. for example..the dog ate the cat

index(0) = the
index(1) = dog
.....so on

how do i do this?? please help me out.


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: having troubles in vector Posted: Apr 10, 2002 11:33 PM
Reply to this message Reply
just read the documenation of vector...

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: having troubles in vector Posted: Apr 10, 2002 11:57 PM
Reply to this message Reply
Wrong, wrong, wrong! Don't use a Vector, use a List.
List words = new ArrayList();
words.add("Go");
words.add("placidly");
words.add("amid");
words.add("the");
words.add("noise");
words.add("and");
words.add("haste");
You can use the antiquated Vector in place of ArrayList if you really insist, or really need to -- for example, if you are using and old version of Java or a subset that doesn't have the new collections.

nagu

Posts: 7
Nickname: nagu
Registered: Apr, 2002

Re: having troubles in vector Posted: Apr 11, 2002 12:01 AM
Reply to this message Reply
You can solve your problem using StringTokenizer without any difficulty.u can parse the whole string with space as delimeter and later u can retrieve the nodes of the string.

Hope this will clear u.

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: having troubles in vector Posted: Apr 11, 2002 6:22 AM
Reply to this message Reply
Tell me, what's wrong with using a Vector?

Thanks!

Lynn.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: having troubles in vector Posted: Apr 11, 2002 7:03 AM
Reply to this message Reply
There is nothing wrong in using Vector here. The only
difference between the add method of Vector and the
add method of ArrayList is that in Vector add method
is synchronized whereas in ArrayList it is not. I know synchronization will make add in Vector a bit slower, which is really insignificant for 4-5 elements to add . Since Dani has not mentioned anything about synchronization it is not correct to say that it is wrong to use Vector and Vector is antiquated.
I am sure this question has been posted by a student, who is working on his assignment. We should give him
an easy solution (If you can!!!) and not start teaching him what is hot in Java and what is antiquated. Here is the code for add method in Vector:
/**
     * Appends the specified element to the end of this Vector.
     *
     * @param o element to be appended to this Vector.
     * @return true (as per the general contract of Collection.add).
     * @since JDK1.2
     */
    public synchronized boolean add(Object o) {
	modCount++;
	ensureCapacityHelper(elementCount + 1);
	elementData[elementCount++] = o;
        return true;
    }

Here is the code for add method for ArrayList:
 /**
     * Appends the specified element to the end of this list.
     *
     * @param o element to be appended to this list.
     * @return <tt>true</tt> (as per the general contract of Collection.add).
     */
    public boolean add(Object o) {
	ensureCapacity(size + 1);  // Increments modCount!!
	elementData[size++] = o;
	return true;
    }


Now it is upto you to decide why it is wrong to use Vector in Dani's case.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: having troubles in vector Posted: Apr 11, 2002 12:11 PM
Reply to this message Reply
You obviously misapprehended the salient point: use an interface as the reference, not the concrete object's type, when possible. That is, use a List reference, whether the object is a Vector, ArrayList, LinkedList, or whatever. That makes for more flexible code.

As far as using a Vector versus an ArrayList or LinkedList, that is dependent on the problem at hand. You can often use any implementation of List and it will work; you will only really need to pick the right one if it starts causing performance problems (however, using the List interface as your reference will make it much easier to switch implementations). I would use an ArrayList for a simple list that may grow, a LinkedList for a list that may often have insertions and deletions at random locations and a Vector when I need it to be always serialized. So for a simple homework assignment type problem, I would use an ArrayList, since synchronizing data access is rarely a problem in single-threaded programs.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: having troubles in vector Posted: Apr 11, 2002 1:19 PM
Reply to this message Reply
Matt, I understand data structures and I know which one to use where. What I wanted to tell you is that our answer to anyone's question should be according to the person's understanding who is asking the question. Most of the times, you can tell the level of the person who is asking the question. A student who is having trouble in parsing a string will have problem in understanding concept of synchronization and multi-threading and difference between List, ArrayList and Vector. If someone asks a question on multi-threading and he is using Vector or ArrayList then your points are good. In absolute sense, your points are valid. But, in this case, when a student is asking you about how to parse a string, he would be better helped if you tell him how to accomplish what he wants. Don't take it otherwise. We should not argue about such silly points. We should help each other and that is why we come to this forum.

Thanks
Kishori

Flat View: This topic has 7 replies on 1 page
Topic: Date Previous Topic   Next Topic Topic: SMS

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use