简体   繁体   English

如何迭代对象的数组列表以查找指定的对象

[英]How to Iterate a arraylist of objects to find a specified object

I am having trouble understanding and getting this function of my program to work. 我在理解和使程序的此功能无法正常工作时遇到了麻烦。 I am creating a library where I first input multiple objects into an array-list. 我正在创建一个库,在该库中我首先将多个对象输入到数组列表中。 The objects consist of title and format of the media. 对象包括媒体的标题和格式。 I need to be able to search through the objects in my array-list to find a specific object and mark that object as "checked out". 我需要能够搜索数组列表中的对象以找到特定的对象,并将该对象标记为“已签出”。 I have been researching iterators and trying to understand how to make them find a specified title within an object but I am having trouble. 我一直在研究迭代器,并试图了解如何使它们在对象中找到指定的标题,但是我遇到了麻烦。 I am getting MediaItem@3c4568f8 when I attempt to println(it.next()); 当我尝试println(it.next())时,我得到MediaItem @ 3c4568f8; so I know there is a problem with returning the correct format of information. 因此我知道返回正确的信息格式存在问题。 Any help on how to search through the objects in my items array-list would be greatly appreciated. 任何有关如何搜索我的项目array-list中的对象的帮助将不胜感激。

import java.util.Iterator;
import java.util.Scanner;
import java.util.ArrayList;

public class Library {

    static ArrayList<MediaItem> items = new ArrayList<MediaItem>();
    static int menuOption;
    static Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {
        String title, format, loanedTo, dateLoaned;
        boolean right = false;

        do {
            displayMenu();
            if (menuOption == 1) {
                System.out.println("Enter Title: ");
                title = scan.next();
                System.out.println("Enter format: ");
                format = scan.next();
                addNewItem(title, format);
            } else if (menuOption == 2) {
                System.out.println("Enter the item title");
                title = scan.next();
                System.out.println("Who are you loaning it to?");
                loanedTo = scan.next();
                System.out.println("When did you loan it to them?");
                dateLoaned = scan.next();
                markItemOnLoan(title, loanedTo, dateLoaned);
            } else if (menuOption == 3) {
                for (MediaItem mi : items) {
                    System.out.println(mi.getTitle() + ", " + mi.getFormat());
                }
            } else if (menuOption == 4) {

            } else {
                System.exit(1);
            }

        } while (!right);
    }

    static int displayMenu() {
        boolean right = false;

        do {

            System.out.println("Menu: ");
            System.out.println("1. Add New Item");
            System.out.println("2. Mark an item as on loan");
            System.out.println("3. List all items");
            System.out.println("4. Mark an item as returned");
            System.out.println("5. Quit");
            menuOption = scan.nextInt();

            if (menuOption < 1 || menuOption > 5) {
                System.out.println("Invalid Number!");
            }

            return menuOption;
        } while (!right);
    }

    static void addNewItem(String title, String format) {
        MediaItem b = new MediaItem();
        b.setTitle(title);
        b.setFormat(format);
        items.add(b);

    }

    static void markItemOnLoan(String title, String name, String date) {
        Iterator<MediaItem> it = items.iterator();
        System.out.println(it.next());
    }

}





public class MediaItem {

String title;
    String format;
    boolean onLoan;
    String loanedTo;
    String dateLoaned;

    MediaItem() {
        title = null;
        format = null;
        onLoan = false;
        loanedTo = null;
        dateLoaned = null;

    }

    MediaItem(String title, String format) {
        title = new String();
        format = new String();

    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public boolean isOnLoan() {
        return onLoan;
    }

    public void setOnLoan(boolean onLoan) {
        this.onLoan = onLoan;
    }

    public String getLoanedTo() {
        return loanedTo;
    }

    public void setLoanedTo(String loanedTo) {
        this.loanedTo = loanedTo;
    }

    public String getDateLoaned() {
        return dateLoaned;
    }

    public void setDateLoaned(String dateLoaned) {
        this.dateLoaned = dateLoaned;
    }

    void markOnLoan(String name, String date) {
        onLoan = true;
    }

    void markReturned() {
        onLoan = false;
    }
}

The MediaItem@3c4568f8 output is the result of Object 's toString() method . MediaItem@3c4568f8输出是ObjecttoString()方法的结果

[T]his method returns a string equal to the value of: [T]他的方法返回的字符串等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode()) 

You haven't overridden toString() in Media , so it inherits the method from Object . 您没有在Media重写toString() ,因此它从Object继承了该方法。

Override toString() , returning the string that you want printed when your object is passed to System.out.println and string conversion calls toString() on your Object . 覆盖toString() ,返回将对象传递到System.out.println时要打印的字符串,并且字符串转换调用Object上的toString()

You should implement a toString() method that will return the something like title instead of the object reference. 您应该实现一个toString()方法,该方法将返回诸如title之类的内容而不是对象引用。 Also, you can implement a comparator to accurately determine if the objects match. 另外,您可以实现一个比较器以准确确定对象是否匹配。

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

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