Solved

Returning vector

Posted on 2003-11-27
9
273 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 69
factorial example 4 41
hibernate example issues from command prompt 10 42
jsp insert to database example 2 25
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 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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

823 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