Solved

Returning vector

Posted on 2003-11-27
9
270 Views
Last Modified: 2010-03-31
I created the following vector(bookauthorVec) in book class and trying to return in Library class. It compiles, but nothing appears in JList(booklist). I already tested getBookAuthor() with main and it works. When I created vector(bookauthorVec) inside of Library class it worked. If I return string from Book class and store to vecBookAuthor in Library class it also worked.(It appears in JList(booklist).
-----------
Book class
------------
public Vector getBookAuthor(){
      for(int i = 0; i < bhm.size() ; i++){
     bookauthorVec.add(bhm.get(titleVec.elementAt(i).toString()));
    }
return bookauthorVec;
 }
---------------------
Library class
-------------------
Vector vecBookAuthor = new Vector();
Book book = new Book();
vecBookAuthor = book.getBookAuthor() ;

JList bookList = new JList(vecBookAuthor);


Any idea?
0
Comment
Question by:dkim18
  • 5
  • 2
  • 2
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 175 total points
Comment Utility
firstly you are better off doing:
Book book = new Book();
Vector vecBookAuthor = book.getBookAuthor() ;

If you want to add things later to the list then you should use a Vector to create it and should instead use a DefaultListModel.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Why don't you simply return bhm from getBookAuthor?
0
 

Author Comment

by:dkim18
Comment Utility
objects;

firstly you are better off doing:
Book book = new Book();
Vector vecBookAuthor = book.getBookAuthor() ;
>>why?

then how do I access Book class' public function from Library class?
0
 
LVL 92

Accepted Solution

by:
objects earned 175 total points
Comment Utility
> Vector vecBookAuthor = new Vector();

here you create a new empty vector

> Book book = new Book();
> vecBookAuthor = book.getBookAuthor() ;

And here you assign the variable a new value.
So the vector you created in the first line is unused.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
Comment Utility
Where do actually add the elements to bhm?
Unless you add them somehow in the constructor, then it will be empty. Which is why the list is empty.

I would think you should be passing the book details to its constructor.
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 75 total points
Comment Utility
Might also create a problem when you add some more elements to the bhm Vector and again call the getBookAuthor () method, because then you will end up adding all the previously added elements again. Where are you initializing the bhm and bookauthorVec Vectors, and adding data to the bhm Vector?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> bhm.get(titleVec.elementAt(i).toString())

Oh, so there's a titleVec too. By the way, what is the data-type of bhm? Is it a Vector too (initially, it looked like). But Vector does not have a get (String) method if I'm right. It only has a get (int) method.
0
 

Author Comment

by:dkim18
Comment Utility
thank you for all your help. I stored objects in vector and then put it in JList.
I already submitted this proj and working on next one.
Again, your comment help me a lot.
thanks,
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now