开发者

Using ArrayList to store variables

I'm coding a project to store an array of objects, probably in an arraylist.

So, my object has data elements, getters and setters. I do not have an idea how to access the elements of the object.

For example my object is a book which has the following data elements.

Book
->Author
->Price
->Date

If the price of a certain book changes, how do I update the arrayList? Do I need a 2d arrayList? I th开发者_Go百科ink indexOf would not work because it is an object? Not really sure.

I'm just a newbie in programming, so I'm not sure if arrayList is the right data structure to use. Thank you very much for reading this, your help will be appreciated.


You can access an object in an ArrayList by it's index. Say you want the third book:

List<Book> lisOfBooks = someFunctionWhichGetsListOfBooks();

Book book = listOfBooks.get(2);

(Remember indexes start at 0 in java) Then you can do:

String Author = book.getAuthor();

or to set the price:

book.setPrice(newPrice);

If you do not know where in the list the particular book is you have 2 choices: 1) iterate through the list until you find the book you want or 2) you can use a HashMap. But you need to decide what to key the book on (what makes each book unique?). Let's say you'll be looking up books by id. Then you can put all your books in a HashMap keyed by an Integer id:

HashMap<Integer, Book> bookHash = new HashMap<Integer, Book>();

for (Book book : listOfBooks) {
    bookHash.put(book.getId(), book);
}

Then to retrieve any book you can do:

Book book = bookHash.get(bookId);

Obviously this assumes your Book class has appropriate getter and setter methods. E.g:

public class Book {

   private int id;
   private String author;
   ...

   public String getAuthor() {
      return author;
   }

   public void setPrice(price) {
     this.price = price;
   }

   ...

} 


An ArrayList is a good implementation if you need to store your objects in a list. A good alternative for a collection of books: a Map implementation. This is useful if every book can be identified by a unique key (name, ISBN number, ...) and you use that key to "get" a book from the collection.

A book is an object on its on and should be modeled with a Book class. So if you want to store books in list structure, this would be OK:

List<Book> books = new ArrayList<Book>();
books.add(new Book("Robert C. Martin","Clean Code"));
books.add(new Book("Joshua Bloch","Effective Java"));

For a map, a code fragment could look like this:

Map<String, Book> books = new HashMap<String, Book>();
books.put("978-0132350884", new Book("Robert C. Martin","Clean Code"));
books.put("978-0321356680", new Book("Joshua Bloch","Effective Java"));

Edit - finding a book and updating the price

With a list, you have to iterate through the list until you find the object that represents the book you're looking for. It's easier with the map shown above, if you always can use the ISBN, for example. With the list, it goes like this:

List<Book> books = getBooks();  // get the books from somewhere
for (Book book:books) {
   if (matchesSearchCriteria(book)) {  // some method to identify a book, placeholder!
      book.setPrice(newPrice);         // modify the books price
      break;
   }
}


You need to create a custom "datatype", basically a POJO (Plain old java object) that will have fields for all the parameters you want. Let's call it Book:

class Book {
  public String author;
  public double price;
  public Date date;
}

Of course it's better to have private fields and encapsulate with getters and setters, i jsut wrote that for simplicity.

Then you can create a List of Book objects.

List<Book> books = new ArrayList<Book>();

And add Books to it which you can then later iterate through like this:

for (Book book : books) {
  System.out.println(book.getAuthor());
}


I'd be looking at some sort of lookup data structure like a hashmap, for example

HashMap<String, Hashmap<String,String>> books = new Hashmap<String, HashMap<String, String>>();
HashMap<String, String> bookelements = new HashMap<String,String>();
bookelements.put("Author","J.K Rowling");
bookelements.put("Price","£5");
bookelements.put("Year","2000");
books.put("harry potter",bookelements);

//Get elements like so
String author = (String)books.get("harry potter").get("Author");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜