user3921
user3921

Reputation: 241

Accessing an object class variable in ArrayList (java)

So, let's say i have a class to describe a book

   public class Book {
     String name;
     int pages;
     String auother;
     boolean available;
     String rentername;
  }

Now, i have set an array list to contain the unknown numbers of books i wish to have/add/delete during the running time. The thing is, when i try to accesses a certain book in the arraylist by an index i get an error.

enter code here

ArrayList Books = new ArrayList();
Book bk1 = new Book();
Books.add(bk1);
System.out.println(Books[0]. --->>> won't give me accesses to the 'Book' class variables (name, pages...)

So, how can i make it to the class variables? Thanks.

Upvotes: 1

Views: 70342

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347332

ArrayList is an implementation of a java.util.List collection backed by a dynamic array.

This means three things.

Firstly, you don't need to worry about resizing, allocating or coping elements into the array, the ArrayList will take care of it for you.

Secondly, any method that wants a List doesn't care about the implementation (you could pass a LinkedList instead) which decouples the code (meaning you can change the implementation without adversely effecting those who rely on it, because of the contract made through the List interface)

Thirdly, to interact with the contents of the List, you need to use one or more of the interfaces access methods.

You already know about add, but to obtaining an object, you need to use get which;

Returns the element at the specified position in this list

You might also be interested in

There are other methods, of course, but these are the immediately useful.

I'd also have a read through the Collections Trail

UPDATED example

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {

        List<Book> listOfBooks = new ArrayList<Book>();
        Book bk1 = new Book();
        listOfBooks.add(bk1);

        System.out.println("           bk1 = " + bk1);
        System.out.println("listOfBooks(0) = " + listOfBooks.get(0));

    }

    public class Book {

        String name;
        int pages;
        String auother;
        boolean available;
        String rentername;

        @Override
        public String toString() {
            return "Book: name = " + name + "; pages = " + pages + "; available = " + available + "; rentername = " + rentername + "; hashCode = " + hashCode();
        }

    }
}

Which outputs

           bk1 = Book: name = null; pages = 0; available = false; rentername = null; hashCode = 1137551390
listOfBooks(0) = Book: name = null; pages = 0; available = false; rentername = null; hashCode = 1137551390

Upvotes: 12

Baz
Baz

Reputation: 36904

Use the get(int index) method of List to get the entry you want:

System.out.println(Books.get(0));

You cannot access the entries of a List with the notation used for arrays.

BTW: Please stick to the Java Naming Conventions

Upvotes: 6

Jigar Joshi
Jigar Joshi

Reputation: 240996

You need to create public setters and getters method to access fields, and to access book instance you need to

books.get(index)

Upvotes: 2

Related Questions