Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何迭代对象的arraylist以查找指定的对象_Java_Object_Arraylist_Iterator - Fatal编程技术网

Java 如何迭代对象的arraylist以查找指定的对象

Java 如何迭代对象的arraylist以查找指定的对象,java,object,arraylist,iterator,Java,Object,Arraylist,Iterator,我在理解和使用我的程序的这个功能方面有困难。我正在创建一个库,在这里我首先将多个对象输入到一个数组列表中。对象包括媒体的标题和格式。我需要能够搜索数组列表中的对象以找到特定对象,并将该对象标记为“已签出”。我一直在研究迭代器,试图理解如何让它们在对象中找到指定的标题,但我遇到了麻烦。我越来越MediaItem@3c4568f8当我尝试打印ln(it.next())时;所以我知道返回正确格式的信息有问题。对于如何在我的项目数组列表中搜索对象的任何帮助,我们将不胜感激 import java.uti

我在理解和使用我的程序的这个功能方面有困难。我正在创建一个库,在这里我首先将多个对象输入到一个数组列表中。对象包括媒体的标题和格式。我需要能够搜索数组列表中的对象以找到特定对象,并将该对象标记为“已签出”。我一直在研究迭代器,试图理解如何让它们在对象中找到指定的标题,但我遇到了麻烦。我越来越MediaItem@3c4568f8当我尝试打印ln(it.next())时;所以我知道返回正确格式的信息有问题。对于如何在我的项目数组列表中搜索对象的任何帮助,我们将不胜感激

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;
    }
}
import java.util.Iterator;
导入java.util.Scanner;
导入java.util.ArrayList;
公共班级图书馆{
静态ArrayList项=新ArrayList();
静态整数选择;
静态扫描仪扫描=新扫描仪(System.in);
公共静态void main(字符串参数[]){
字符串标题、格式、借出日期、借出日期;
布尔右=假;
做{
显示菜单();
如果(菜单选项==1){
System.out.println(“输入标题:”);
title=scan.next();
System.out.println(“输入格式:”);
format=scan.next();
addNewItem(标题、格式);
}else if(菜单选项==2){
System.out.println(“输入项目标题”);
title=scan.next();
System.out.println(“你把它借给谁了?”);
loanedTo=scan.next();
你什么时候借给他们的;
dateLoaned=scan.next();
markItemOnLoan(所有权、借出人、借出日期);
}else if(菜单选项==3){
对于(MediaItem mi:items){
System.out.println(mi.getTitle()+”,“+mi.getFormat());
}
}else if(菜单选项==4){
}否则{
系统出口(1);
}
}而(对),;
}
静态int显示菜单(){
布尔右=假;
做{
System.out.println(“菜单:”);
System.out.println(“1.添加新项”);
System.out.println(“2.将项目标记为借出”);
System.out.println(“3.列出所有项目”);
System.out.println(“4.将项目标记为已退回”);
System.out.println(“5.Quit”);
menuOption=scan.nextInt();
如果(菜单选项<1 | |菜单选项>5){
System.out.println(“无效数字!”);
}
返回菜单选项;
}而(对),;
}
静态void addNewItem(字符串标题、字符串格式){
MediaItem b=新的MediaItem();
b、 片名(片名);
b、 setFormat(格式);
增加(b)项;
}
静态void markItemOnLoan(字符串标题、字符串名称、字符串日期){
Iterator it=items.Iterator();
System.out.println(it.next());
}
}
公共类MediaItem{
字符串标题;
字符串格式;
布尔onLoan;
借出的字符串;
贷款;
MediaItem(){
title=null;
格式=空;
onLoan=false;
loanedTo=null;
dateLoaned=null;
}
MediaItem(字符串标题、字符串格式){
title=新字符串();
格式=新字符串();
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getFormat(){
返回格式;
}
公共void setFormat(字符串格式){
this.format=格式;
}
公共布尔isOnLoan(){
返回onLoan;
}
public void setOnLoan(布尔onLoan){
this.onLoan=onLoan;
}
公共字符串getLoanedTo(){
归还贷款给;
}
public void setLoanedTo(字符串loanedTo){
this.loanedTo=loanedTo;
}
公共字符串getDateLoaned(){
归还贷款;
}
公共无效setDateLoaned(字符串dateLoaned){
this.dateLoaned=dateLoaned;
}
void markOnLoan(字符串名称、字符串日期){
onLoan=true;
}
void markReturned(){
onLoan=false;
}
}

MediaItem@3c4568f8
输出是测试的结果

[T] 他的方法返回一个等于以下值的字符串:

getClass().getName() + '@' + Integer.toHexString(hashCode())
您没有在
媒体中重写
toString()
,因此它从
对象继承了该方法


重写
toString()
,返回对象传递到
System.out.println
时要打印的字符串,并在
对象上调用
toString()

MediaItem@3c4568f8
输出是测试的结果

[T] 他的方法返回一个等于以下值的字符串:

getClass().getName() + '@' + Integer.toHexString(hashCode())
您没有在
媒体中重写
toString()
,因此它从
对象继承了该方法


重写
toString()
,返回对象传递到
System.out.println
时要打印的字符串,并在
对象上调用字符串转换
toString()

您应该实现一个toString()方法,该方法将返回类似标题的内容,而不是对象引用。此外,您还可以实现一个比较器来准确地确定对象是否匹配。

您应该实现一个toString()方法,该方法将返回类似title的内容,而不是对象引用。此外,您还可以实现一个比较器来精确确定对象是否匹配。

您的对象没有
toString()
方法,因此它将对象引用作为字符串返回。换句话说,它调用的是
Object.toString()
,而不是
MediaItem.toStri