简体   繁体   English

访问ArrayList中的对象类变量(java)

[英]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. 问题是,当我尝试通过索引访问arraylist中的特定书籍时,出现错误。

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. 谢谢。

ArrayList is an implementation of a java.util.List collection backed by a dynamic array. ArrayList是由动态数组支持的java.util.List集合的实现。

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. 首先,您不必担心将元素的大小调整,分配或处理到数组中, ArrayList将为您处理。

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) 其次,任何需要List方法都不关心实现(您可以传递LinkedList ),这会使代码解耦(意味着您可以更改实现而不会对依赖它的人产生不利影响,因为通过List界面)

Thirdly, to interact with the contents of the List , you need to use one or more of the interfaces access methods. 第三,要与List的内容进行交互,您需要使用一种或多种接口访问方法。

You already know about add , but to obtaining an object, you need to use get which; 您已经了解了add ,但是要获取对象,您需要使用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 我还将通读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

Use the get(int index) method of List to get the entry you want: 使用Listget(int index)方法获取所需的条目:

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

You cannot access the entries of a List with the notation used for arrays. 您不能使用用于数组的符号访问List的条目。

BTW: Please stick to the Java Naming Conventions 顺便说一句:请遵守Java命名约定

您需要创建公共的setter和getter方法来访问字段,并需要访问图书实例

books.get(index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM