gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:15:47 27 4
gpt4 key购买 nike

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

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;
}
}

最佳答案

MediaItem@3c4568f8 输出是 Object's toString() method 的结果。

[T]his method returns a string equal to the value of:

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

您尚未重写 Media 中的 toString(),因此它继承了 Object 的方法。

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

关于java - 如何迭代对象的数组列表以查找指定的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22236679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com