我正在尝试检查链接列表中是否存在对象,并根据它是否存在执行操作,但是,无论我做什么,java都会将所有对象视为不同。下面提供了主要代码,我很确定逻辑中的错误是在这段代码中。文章和客户类非常标准。如果列表包含带有标题的文章,则该标志变量应为true,但始终为false。任何帮助将不胜感激。
import java.util.*;
import java.io.*;
public class Proj1 {
public static void main(String[] args) throws FileNotFoundException {
LinkedList<Article> Articles = new LinkedList<Article>();
LinkedList<Customer> Customers = new LinkedList<Customer>();
ListIterator<Customer> it = Customers.listIterator();
int id = 0;
String command = "";
if (args.length == 0 || args[0] == null) {
System.out.println("Please give a valid command file");
} else {
try {
Scanner reader = new Scanner(new FileInputStream(args[0]));
while (reader.hasNext()) {
String arg = reader.nextLine();
arg.split(" ");
String[] commands = arg.split("\\s+");
if (isInt(commands[0])) {
id = Integer.parseInt(commands[0]);
command = commands[1];
Customer temp = new Customer(id);
if (Customers.size() == 0) {
Customers.add(temp);
} else {
boolean flag = false;
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
flag = true;
}
}
if (flag == false) {
Customers.add(temp);
}
}
} else {
command = commands[0];
}
// System.out.println(id+" "+command);
if (command.equalsIgnoreCase("borrow")) {
String title = "";
int x = commands.length;
boolean flag = false;
for (int j = 2; j < x; j++) {
title += commands[j] + " ";
}
Article Article = new Article(title);
System.out.println(Articles.size());
if (Articles.size() == 0) {
Articles.add(Article);
} else {
for (int i = 0; i < Articles.size(); i++) {
if (Article.getTitle() == Articles.get(i).getTitle()) {
flag = true;
}
}
if (flag == false) {
Articles.add(Article);
}
}
System.out.println(flag);
for (int i = 0; i < Customers.size(); i++) {
if (Customers.get(i).CustomerList().contains(title) && flag == true) {
Article.addToQ(Customers.get(i));
} else {
Customers.get(i).CustomerBorrow(Article);
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("return")) {
String title = "";
int x = commands.length;
for (int j = 2; j < x; j++) {
title += commands[j] + " ";
}
Article Article = new Article(title);
if (Articles.size() == 0) {
Articles.add(Article);
} else {
boolean flag = false;
for (int i = 0; i < Articles.size(); i++) {
if (title == Articles.get(i).getTitle()) {
flag = true;
}
}
if (flag == false) {
Articles.add(Article);
}
}
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
Customers.get(i).CustomerReturn(Article);
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("list")) {
for (int i = 0; i < Customers.size(); i++) {
if (id == Customers.get(i).getId()) {
System.out.println("Customer " + id
+ " currently has: "
+ Customers.get(i).CustomerList());
}
}
} else if (command.equalsIgnoreCase("whohas")) {
String title = "";
int x = commands.length;
for (int i = 1; i < x; i++) {
title += commands[i] + " ";
}
boolean flag = false;
int tempId = 0;
for (int i = 0; i < Customers.size(); i++) {
tempId = Customers.get(i).getId();
if (Customers.get(i).CustomerList().contains(title)) {
flag = true;
tempId = Customers.get(i).getId();
}
}
if (flag = true) {
System.out.println(tempId + " currently has "
+ title);
} else {
System.out
.println("Currently no one has checked out "
+ title);
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("waitlist")) {
String title = "";
int x = commands.length;
for (int i = 1; i < x; i++) {
title += commands[i] + " ";
}
for (int i = 0; i < Customers.size(); i++) {
if (Customers.get(i).CustomerList().contains(title)) {
Articles.get(i).printQ();
}
}
// System.out.println(title);
} else if (command.equalsIgnoreCase("listCustomers")) {
System.out.println("Customers include: ");
for (int i = 0; i < Customers.size(); i++) {
System.out.println(Customers.get(i).getId());
}
} else {
System.out.println("Command not recognized");
}
}
reader.close();
}
catch (Exception e) {
System.out.println("command not formatted correctly");
}
}
}
public static boolean isInt(String string) {
try {
Integer.parseInt(string);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
}
等命令
29借“纽约时报” 29借“纽约时报”允许重复,我试图避免这种情况。感谢。
答案 0 :(得分:2)
可能是那个
if (Article.getTitle() == Articles.get(i).getTitle()) {
打算比较字符串?这可以解释为什么你的旗帜总是错误的。要比较Java中的字符串,您应该使用equals
(或equalsIgnoreCase
进行不区分大小写的比较)
if (Article.getTitle().equals(Articles.get(i).getTitle()) {
更多背景信息here
答案 1 :(得分:1)
我只是在猜测,但我敢打赌你要么没有覆盖equals
和hashCode
类中的Customer
和Article
,要么你没有正确地做到这一点。
Joshua Bloch在第3章“有效Java”中向您展示了如何。
我也想知道为什么如果不允许重复,你没有选择Set
数据结构。