Solved

Returning vector

Posted on 2003-11-27
9
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 175 total points
ID: 9834518
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
ID: 9834525
Why don't you simply return bhm from getBookAuthor?
0
 

Author Comment

by:dkim18
ID: 9834554
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 92

Accepted Solution

by:
objects earned 175 total points
ID: 9834941
> 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
 
LVL 92

Expert Comment

by:objects
ID: 9834946
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:Mayank S
Mayank S earned 75 total points
ID: 9835103
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:Mayank S
ID: 9835119
>> 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
ID: 9842475
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
ID: 9843363
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

691 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