Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

ArrayLists as a return type


  • Please log in to reply
9 replies to this topic

#1 gabe86

gabe86

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Local time:08:21 AM

Posted 08 October 2011 - 10:17 PM

Is anyone able to help me with this problem? I have a written a class called book, then a class called bookstore which creates an arraylist of book objects
i have to include a method with this as the method signature public ArrayList<Book> searchByTitle(String search)
what do you put in for the return statement? i've written this code for the method but the compiler says its in compatible types

public ArrayList<Book> searchByTitle(String search)
  {	  
    BookStore searchMatches = new BookStore();
    
    for(Book b: books)
	 {
	   if(b.getTitle().contains(search))
		  searchMatches.addBook('B');
	 }
	 return searchMatches;
  }
nb edit: the commas are around the b to stop it becoming a smiley

Edited by groovicus, 09 October 2011 - 09:56 AM.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:21 AM

Posted 09 October 2011 - 10:05 AM

The method signature indicates that you are returning an array list of type 'Book'. You are trying to return an object of type 'BookStore', which is not an array list. Perhaps your 'BookStore' class has a method to return the actual list of books, or is maybe extends the array list class? Hard to say without seeing your BookStore class.

If you have a method in your BookStore class, then your return statement will be something like:
return searchMatches.getList();
Or something like that.

#3 gabe86

gabe86
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Local time:08:21 AM

Posted 09 October 2011 - 10:46 AM

here is the book & bookstore classes it might make it a bit clearer for you
public class Book
{
  private String title = new String();
  private String author = new String();
  private double price;
  
  // constructors 
  public Book()
  {
    title="";
	 author="";
	 price=0.0;
  }
  
  public Book(String t, String a, double p)
  {
    title=t;
	 author=a;
	 price=p;
  }
  
  public String getTitle()
  {
    return title;
  }
  
  public String getAuthor()
  {
    return author;
  }
  
  public double getPrice()
  {
    return price;
  }
  
  public String toString()
  {
    return title+"  "+author+"  "+price;
  }
  
}//end class 


import java.util.*;

public class BookStore
{
  ArrayList <Book> books;
  
  public BookStore()
  {
    books = new ArrayList <Book>();
  }
  
  public void addBook(Book <img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />
  {
    books.add(<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;
  }
  
  public void listAll()
  {
    for(Book b: books)
	   System.out.println(b.toString());
  }

  public ArrayList<Book> searchByTitle(String search)
  {	  
    BookStore searchMatches = new BookStore();
    
    for(Book b: books)
	 {
	   if(b.getTitle().contains(search))
		  searchMatches.addBook(<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;
	 }
	 return searchMatches;
  }
  
}//end class

i was looking at arraylists in the api but can't see how to return them. i tried this
return searchMatches.toArray();
but it didn't work either

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:21 AM

Posted 09 October 2011 - 12:26 PM

Of course it didn't work. Your class does not have a method called 'toArray'. :)

Do you understand why your method is failing? You need to return the array list. Try something like
return searchMatches.myBooksList;

Edited by groovicus, 09 October 2011 - 12:31 PM.


#5 gabe86

gabe86
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Local time:08:21 AM

Posted 09 October 2011 - 01:11 PM

Of course it didn't work. Your class does not have a method called 'toArray'. :)

yeah i know the class doesn't have that method but the ArrayList class does, i was just trying it to see

Do you understand why your method is failing? You need to return the array list. Try something like

return searchMatches.myBooksList;

i don't know how to return the arraylist. i included this
BookStore searchMatches = new BookStore();
so that i would have an ArrayList of books called searchMatches to return but it wont work. Do i have to write a method to call on searchMatches to make it return as an ArrayList?

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:21 AM

Posted 09 October 2011 - 04:55 PM

Ok, one more time with feeling.

1. You have a method that is defined to have a return type of ArrayList.
2. You instantiate an Object of type 'BooksStore' called 'searchMatches'.
3. searchMatches has a method called addBook which adds a 'Book' to the 'Books' Array List.
4. You are returning searchMatches, which is not an array list (and since it is not an array list, it does not have a method called 'toArray'). However, it does have a member called 'Books' which is an array list.
5. You need to figure out how to return the array list.

Edited by groovicus, 09 October 2011 - 04:56 PM.


#7 gabe86

gabe86
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Local time:08:21 AM

Posted 09 October 2011 - 08:58 PM

on point 4. is every BookStore object created not an ArrayList of Book obj.? going by the constructor.
point 5 is why i posted here, i've never returned an ArrayList before & need to know what way to go about doing it

#8 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:21 AM

Posted 10 October 2011 - 04:47 AM

on point 4. is every BookStore object created not an ArrayList of Book obj.? going by the constructor.

No. Every BookStore object contains an ArrayList called Books. Constructors create an instance of a class. In your case, they create an instance of BookStore called searchMatches. The constructor also happens to instantiate an ArrayList called Books. That doesn't make the class an ArrayList.

i've never returned an ArrayList before & need to know what way to go about doing it


I am not just going to tell you because this is something you need to figure out and understand. Once you understand it will be so blatantly obvious what you need to do that you will no longer need help with this particular concept. Returning an ArrayList is no different than returning any other Object.

Right now, you are returning an Object. If your method signature was like this:
public BookStore searchByTitle(String search)
You would no longer have a compiler error.

1. What variable name is given to your ArrayList?
2. Return it.

I'll give you a small piece of sample code.

public class Foo
{
   private Int i;
   private String s;

   public Foo()
   {
      int = 5;
      string = "5";
   }

   public String getString()
   {
      return s;
   }

   public Int getInt()
   {
      return i;
   }
}


#9 gabe86

gabe86
  • Topic Starter

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Local time:08:21 AM

Posted 11 October 2011 - 02:06 PM

Ok i finally got it after your last post. I was getting things mixed up a bit, that's the first class i wrote with a constructor like that. this is what worked for me in the end & it was obvious enough after you explained it
return searchMatches.books
thanks for replying & the help

#10 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:21 AM

Posted 11 October 2011 - 03:27 PM

It takes a bit to wrap one's head around some of the concepts. Glad you stuck it out though. :)

Btw, you could have also just done 'return books;' also, since books is a global member.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users