好的我有一个时间表,我有一个数据库中的所有课程,并在加载数据库时显示我现在需要搜索数组列表并显示其上的所有课程内容。例如,我说我搜索math101它显示我的课程名称,ID#,类型等....我需要帮助搜索功能。
package timetable;
import java.sql.*;
import java.util.ArrayList;
public class ExamTimeTableArrayList {
public static class CourseInfo {
private String courseCode;
private String courseName;
private String courseType;
private String courseLevel;
private String faculty;
private String day;
private int session;
CourseInfo(String courseCode, String courseName, String courseType, String courseLevel, String faculty, String day, int session) {
setCourseCode(courseCode);
setCourseName(courseName);
setCourseType(courseType);
setCourseLevel(courseLevel);
setFaculty(faculty);
setDay(day);
setSession(session);
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public void setCourseType(String courseType) {
this.courseType = courseType;
}
public void setCourseLevel(String courseLevel) {
this.courseLevel = courseLevel;
}
public void setFaculty(String faculty) {
this.faculty = faculty;
}
public void setDay(String day) {
this.day = day;
}
public void setSession(int session) {
this.session = session;
}
public String getCourseCode() {
return courseCode;
}
public String getCourseName() {
return courseName;
}
public String getCourseType() {
return courseType;
}
public String getCourseLevel() {
return courseLevel;
}
public String getFaculty() {
return faculty;
}
public String getDay() {
return day;
}
public int getSession() {
return session;
}
}
public static void main(String[] args) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new table().setVisible(true);
}
});
Connection conn = null;
Statement stmt = null;
String Code;
String Name;
String Type;
String Level;
String Faculty;
String Day;
int Session;
int ID;
ArrayList<CourseInfo> array = new ArrayList();
ArrayList<CourseInfo> array2 = new ArrayList(); //Second array for comparison and search
int x = 0;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:Template.sqlite");
conn.setAutoCommit(false);
System.out.println("Database opened successfully");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from Template");
while (rs.next()) {
ID = rs.getInt("ID");
Code = rs.getString("courseCode ");
Name = rs.getString("courseName");
Type = rs.getString("courseType");
Level = rs.getString("courseLevel");
Faculty = rs.getString("Faculty");
Day = rs.getString("Day");
Session = rs.getInt("Session");
CourseInfo Exams = new CourseInfo(Code, Name, Type, Level, Faculty, Day, Session);
Exams.setCourseCode(Code);
Exams.setCourseName(Name);
Exams.setCourseType(Type);
Exams.setCourseLevel(Level);
Exams.setFaculty(Faculty);
Exams.setDay(Day);
Exams.setSession(Session);
array.add(Exams);
x++;
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Operation done successfully");
for (int i = 0; i < 102; i++) {
CourseInfo Object = array.get(i);
System.out.println("Course Code: " + Object.getCourseCode());
System.out.println("Course Name: " + Object.getCourseName());
System.out.println("Course Type: " + Object.getCourseType());
System.out.println("Course Level:" + Object.getCourseLevel());
System.out.println("Faculty: " + Object.getFaculty());
System.out.println("Day: " + Object.getDay());
System.out.println("Session: " + Object.getSession());
System.out.println("\n");
}
}
}
答案 0 :(得分:1)
而不是ArrayList array = new ArrayList();
使用以下代码
Map <String, CourseInfo> courseMap = new HashMap<String, CourseInfo>();
这将定义一个映射来存储针对key的CourseInfo对象,您可以使用courseName作为键。下面的代码可用于向此地图添加条目。
courseMap.put(名称,考试); //这里名字是CourseName。
现在搜索你可以直接使用courseMap.get(Name),这将返回存储在这个地图中的条目,反对courseName。
您可以在此处了解有关地图的详情 http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
答案 1 :(得分:0)
列表不是您要求的相应数据结构。
您可以使用Map并根据唯一键添加记录。 CourseCode或CourseName可用作键,您可以使用此键存储整个对象。