用户输入课程编号,我需要知道如何搜索列表中的对象,看看该输入是否与列表中任何对象的课程编号匹配。
这是控制器代码
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class CourseApp
{
public static void main(String[] args)
{
List<Course> courses = new ArrayList<Course>();
IO IO = new IO();
String number = "";
String name = "";
String FName = "";
String LName = "";
String UName = "";
String title = "";
String author = "";
double price = 0.0;
String reply = "";
String delete = "";
//add objects to list
do {
IO.newLine();
number = IO.courseNumber();
name = IO.courseName();
FName = IO.instrFName();
LName = IO.instrLName();
UName = IO.instrUName();
Instructor tempInstr = new Instructor(LName, FName, UName);
title = IO.bookTitle();
author = IO.bookAuthor();
price = IO.bookPrice();
TextBook tempBook = new TextBook(title, author, price);
Course course = new Course(number, name, tempInstr, tempBook);
courses.add(course);
reply = IO.addAnother();
} while (reply.equalsIgnoreCase ("Y"));
IO.newLine();
//display course
System.out.print(courses.get(0) + "\n\n");
for (int i = 1; i < courses.size(); i++)
{
System.out.println("Press enter to continue...");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
System.out.print(courses.get(i) + "\n\n");
}
//delete course
delete = IO.courseDelete();
int x = 0;
while (x < courses.size())
{
Course course = new Course(courses.get(x));
if (course.getNumber() == delete)
{
courses.remove(x);
System.out.print("Course deleted.");
}
else
{
x++;
}
}
}
}
课程班的代码
public class Course
{
private String courseNumber; // e.g. CIS 200
private String courseName; // e.g. Programming Fundamentals
private Instructor instructor; // Course instructor (object)
private TextBook textBook; // Required Course textbook (object)
/**
This constructor initializes the courseName,
instructor, and text fields.
@param name The name of the course.
@param instructor An Instructor object.
@param text A TextBook object.
*/
public Course(String number, String name, Instructor instr,
TextBook text)
{
// Assign the courseNumber.
courseNumber = number;
// Assign the courseName.
courseName = name;
// Create a new Instructor object, passing
// instr as an argument to the copy constructor.
instructor = new Instructor(instr);
// Create a new TextBook object, passing
// text as an argument to the copy constructor.
textBook = new TextBook(text);
}
public Course(Course x)
{
courseNumber = x.courseNumber;
courseName = x.courseName;
instructor = x.instructor;
textBook = x.textBook;
}
/**
getName method
@return The name of the course.
*/
public String getName()
{
return courseName;
}
/**
getNumber method
@return The number of the course.
*/
public String getNumber()
{
return courseNumber;
}
/**
getInstructor method
@return A reference to a copy of this course's
Instructor object.
*/
public Instructor getInstructor()
{
// Return a copy of the instructor object.
return new Instructor(instructor);
}
/**
getTextBook method
@return A reference to a copy of this course's
TextBook object.
*/
public TextBook getTextBook()
{
// Return a copy of the textBook object.
return new TextBook(textBook);
}
/**
toString method
@return A string containing the course information.
*/
public String toString()
{
// Create a string representing the object.
String str = "Course: " + courseNumber + ", " + courseName + "\n" +
"Instructor: " + instructor + "\n" + "Textbook: " + textBook;
// Return the string.
return str;
}
}
和IO类
/**
* IO handles all input and output for the
* four-square encryption algorithm
*
* <Add remaining required documentation here>
*/
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class IO {
private Scanner s;
/**
* IO sets up a new Scanner to System.in
*/
public IO() {
s = new Scanner(System.in);
}
public String tryCatch(String message)
{
boolean loop = false;
String x = "";
do
{
try
{
System.out.print(message);
x = s.nextLine();
loop = false;
if (x.length() == 0)
{
loop = true;
throw new Exception();
}
}
catch (Exception e)
{
System.out.print("Input cannot be blank! ");
}
} while(loop == true);
return x;
}
/**
* firstKey returns the first key from the user
*
* @return The first key from the user
*/
public String courseNumber() {
return tryCatch("What is the course number? ");
}
/**
* secondKey returns the second key from the user
*
* @return The second key from the user
*/
public String courseName() {
return tryCatch("What is the course name? ");
}
public String instrFName() {
return tryCatch("What is the instructors first name? ");
}
public String instrLName() {
return tryCatch("What is the instructors last name? ");
}
public String instrUName() {
return tryCatch("What is the instructors user name? ");
}
public String bookTitle() {
return tryCatch("What is the title of the book? ");
}
public String bookAuthor() {
return tryCatch("Who is the author of the book? ");
}
public String courseDelete() {
return tryCatch("What is the course number of the course you want to delete? ");
}
public double bookPrice() {
double x = 0.0;
boolean loop = false;
do{
try
{
System.out.print("What is the price of the book? ");
x = Double.parseDouble(s.nextLine());
loop = false;
if (x <= 0.0)
{
throw new Exception();
}
}
catch (NumberFormatException ex)
{
loop = true;
System.out.print("Please enter only numbers! ");
}
catch (Exception e)
{
loop = true;
System.out.print("Please enter only numbers greater than 0! ");
}
} while(loop == true);
return x;
}
public String addAnother() {
return tryCatch("Add another student? ('Y' or 'N'): ");
}
public void newLine()
{
System.out.print("\n");
}
} // end class
以下是我试图编码我所询问的内容的部分
//delete course
delete = IO.courseDelete();
int x = 0;
while (x < courses.size())
{
Course course = new Course(courses.get(x));
if (course.getNumber() == delete)
{
courses.remove(x);
System.out.print("Course deleted.");
}
else
{
x++;
}
}
答案 0 :(得分:0)
@Override public boolean equals(Object obj) {...}
as.data.table(mydata)[, flag := any(
stringr::str_detect(
Acct_Name,
keywords.list[[Segment]])), by = Acct_Name][]
# Acct_Name Segment flag
# 1: joes ski shop All_Other FALSE
# 2: joes alarm shop All_Other TRUE
# 3: joes alarm spot All_Other TRUE
# 4: joes bakery All_Other FALSE
# 5: joes albergue shop Apartments TRUE
# 6: jims Brewery Apartments FALSE
# 7: jims albergue place Apartments TRUE
您删除对象的方法并不安全,相反,您可以将List转换为Iterable,并使用最后一个删除对象,例如:
Object.equals(NewObject)