- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我现在已将所有数据保存到数组中。我如何将其切换为保存在对象的 ArrayList 中?我编写了一个基本的学生对象类,但需要帮助将其切换为保存在数组列表中。非常感谢所有帮助。 arraylist 在第 28 行创建,数据在第 300 行设置。问题是如何修改代码以存储在 ArrayList 中
public class IAdvise extends JFrame {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String[] columnNames = {"ID", "Name", "Major", "GPA", "Campus", "Home Address", "Phone Number", "Email", "Current CST courses", "Past CST course", "Fusture CST courses", "Notes"};
Object[][] data = new Object[60][12];
ArrayList<Student> al = new ArrayList<Student>();
JTable table = new JTable(data, columnNames) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JFrame frame, frame1;
JPanel buttonPanel, buttonPanel2, tablePanel, addPanel, editPanel;
JLabel labelID, labelName, labelMajor, labelGPA, labelCampus, labelAddress, labelPhone, labelEmail,labelCurrent, labelPast, labelFuture, labelNotes;
JTextField txtID, txtName, txtMajor, txtGPA, txtCampus, txtAddress, txtPhone, txtEmail, txtCurrent, txtPast, txtFuture, txtNotes;
JButton btnAdd, btnEdit, btnDelete, btnSort, btnSave, btnAddInput, btnCancel;
int keyCode, rowIndex, rowNumber, noOfStudents;
IAdvise.ButtonHandler bh = new IAdvise.ButtonHandler();
public IAdvise() {
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new IAdvise.RowListener());
table.getColumnModel().getColumn(1).setPreferredWidth(150);
table.getColumnModel().getColumn(2).setPreferredWidth(150);
table.getColumnModel().getColumn(3).setPreferredWidth(50);
table.getColumnModel().getColumn(4).setPreferredWidth(200);
table.getColumnModel().getColumn(5).setPreferredWidth(250);
table.getColumnModel().getColumn(6).setPreferredWidth(150);
table.getColumnModel().getColumn(7).setPreferredWidth(150);
table.getColumnModel().getColumn(8).setPreferredWidth(150);
table.getColumnModel().getColumn(9).setPreferredWidth(150);
table.getColumnModel().getColumn(10).setPreferredWidth(150);
table.getColumnModel().getColumn(11).setPreferredWidth(150);
table.getTableHeader().setResizingAllowed(false);
table.getTableHeader().setReorderingAllowed(false);
JScrollPane scrollPane = new JScrollPane(table);
btnAdd = new JButton("Add Student");
btnAdd.addActionListener(bh);
btnEdit = new JButton("EDIT");
btnEdit.addActionListener(bh);
btnEdit.setEnabled(false);
btnDelete = new JButton("DELETE");
btnDelete.addActionListener(bh);
btnDelete.setEnabled(false);
btnSort = new JButton("Update");
btnSort.addActionListener(bh);
btnSave = new JButton("SAVE");
btnSave.addActionListener(bh);
btnSave.setActionCommand("Save");
btnAddInput = new JButton("Add Student");
btnAddInput.addActionListener(bh);
btnAddInput.setActionCommand("AddInput");
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(bh);
labelID = new JLabel("ID");
labelName = new JLabel("NAME");
labelMajor = new JLabel("Major");
labelGPA = new JLabel("GPA");
labelCampus = new JLabel("Campus");
labelAddress = new JLabel("Address");
labelPhone = new JLabel("Phone Number");
labelEmail = new JLabel("Email");
labelCurrent = new JLabel("Current CST courses");
labelPast = new JLabel("Past CST courses");
labelFuture = new JLabel("Future CST courses");
labelNotes = new JLabel("Notes");
txtID = new JTextField(20);
txtName = new JTextField(20);
txtMajor = new JTextField(20);
txtGPA = new JTextField(20);
txtCampus = new JTextField(20);
txtAddress = new JTextField(20);
txtPhone = new JTextField(20);
txtEmail = new JTextField(20);
txtCurrent = new JTextField(20);
txtPast = new JTextField(20);
txtFuture = new JTextField(20);
txtNotes = new JTextField(20);
txtID.setDocument(new IAdvise.JTextFieldLimit(8));
txtID.addKeyListener(keyListener);
tablePanel = new JPanel();
tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS));
tablePanel.setBorder(BorderFactory.createEmptyBorder(10, 2, 0, 10));
tablePanel.add(table.getTableHeader());
tablePanel.add(table);
JScrollPane jScrollPane = new JScrollPane(tablePanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
scrollPane.setBounds(50, 30, 300, 50);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.add(scrollPane);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
c.fill = GridBagConstraints.HORIZONTAL;
buttonPanel.add(btnAdd, c);
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
buttonPanel.add(btnEdit, c);
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
buttonPanel.add(btnDelete, c);
c.gridx = 0;
c.gridy = 3;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
c.fill = GridBagConstraints.HORIZONTAL;
buttonPanel.add(btnSort, c);
c.gridx = 0;
c.gridy = 4;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
c.fill = GridBagConstraints.HORIZONTAL;
buttonPanel.add(btnSave, c);
frame = new JFrame("Tutoring Database");
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(tablePanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.pack();
addPanel = new JPanel();
addPanel.setLayout(new GridBagLayout());
c.insets = new Insets(1, 0, 1, 1);
c.gridx = 0;
c.gridy = 0;
addPanel.add(labelID, c);
c.gridy = 1;
addPanel.add(labelName, c);
c.gridy = 2;
addPanel.add(labelMajor, c);
c.gridy = 3;
addPanel.add(labelGPA, c);
c.gridy = 4;
addPanel.add(labelCampus, c);
c.gridy = 5;
addPanel.add(labelAddress, c);
c.gridy = 6;
addPanel.add(labelPhone, c);
c.gridy = 7;
addPanel.add(labelEmail, c);
c.gridy = 8;
addPanel.add(labelCurrent, c);
c.gridy = 9;
addPanel.add(labelPast, c);
c.gridy = 10;
addPanel.add(labelFuture, c);
c.gridy = 11;
addPanel.add(labelNotes, c);
// text fields
c.gridx = 1;
c.gridy = 0;
c.ipady = 1;
addPanel.add(txtID, c);
c.gridy = 1;
c.ipady = 1;
addPanel.add(txtName, c);
c.gridy = 2;
c.ipady = 1;
addPanel.add(txtMajor, c);
c.gridy = 3;
c.ipady = 1;
addPanel.add(txtGPA, c);
c.gridy = 4;
c.ipady = 1;
addPanel.add(txtCampus, c);
c.gridy = 5;
c.ipady = 1;
addPanel.add(txtAddress, c);
c.gridy = 6;
c.ipady = 1;
addPanel.add(txtPhone, c);
c.gridy = 7;
c.ipady = 1;
addPanel.add(txtEmail, c);
c.gridy = 8;
c.ipady = 1;
addPanel.add(txtCurrent, c);
c.gridy = 9;
c.ipady = 1;
addPanel.add(txtPast, c);
c.gridy = 10;
c.ipady = 1;
addPanel.add(txtFuture, c);
c.gridy = 11;
c.ipady = 1;
addPanel.add(txtNotes, c);
buttonPanel2 = new JPanel();
buttonPanel2.setLayout(new GridLayout(1, 1));
buttonPanel2.add(btnAddInput);
buttonPanel2.add(btnCancel);
frame1 = new JFrame("Student Database");
frame1.setVisible(false);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame1.add(addPanel, BorderLayout.CENTER);
frame1.add(buttonPanel2, BorderLayout.PAGE_END);
frame1.pack();
}// end
KeyListener keyListener = new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
keyCode = e.getKeyCode();
if (!(keyCode >= 48 && keyCode <= 57) && !(keyCode >= 96 && keyCode <= 105)
&& !(keyCode >= 37 && keyCode <= 40) && !(keyCode == 127 || keyCode == 8)) {
txtID.setEditable(false);
}
}
@Override
public void keyReleased(KeyEvent e) {
txtID.setEditable(true);
}
};
class RowListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent event) {
if (event.getValueIsAdjusting()) {
rowIndex = table.getSelectedRow();
if (data[rowIndex][0] == null || data[rowIndex][0] == "") {
btnEdit.setEnabled(false);
btnDelete.setEnabled(false);
} else {
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);
}
}
}
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Add Student")) {
txtID.setText("");
txtName.setText("");
txtMajor.setText("");
txtGPA.setText("");
txtCampus.setText("");
txtAddress.setText("");
txtPhone.setText("");
txtEmail.setText("");
txtCurrent.setText("");
txtPast.setText("");
txtFuture.setText("");
txtNotes.setText("");
Student student = new Student(txtID.getText(), txtName.getName(), txtMajor.getText(), txtGPA.getText(), txtCampus.getText(), txtAddress.getText(), txtPhone.getText(),txtEmail.getText(), txtCurrent.getText(), txtPast.getText(), txtFuture.getText(), txtNotes.getText());
al.add(student);
frame1.setTitle("Add Student data"); // title bar name for add
frame1.setVisible(true);
} else if (e.getActionCommand().equals("EDIT")) {
txtID.setText(data[rowIndex][0] + "");
txtName.setText(data[rowIndex][1] + "");
txtMajor.setText(data[rowIndex][2] + "");
txtGPA.setText(data[rowIndex][3] + "");
txtCampus.setText(data[rowIndex][4] + "");
txtAddress.setText(data[rowIndex][5] + "");
txtPhone.setText(data[rowIndex][6] + "");
txtEmail.setText(data[rowIndex][7] + "");
txtCurrent.setText(data[rowIndex][8] + "");
txtPast.setText(data[rowIndex][9] + "");
txtFuture.setText(data[rowIndex][10] + "");
txtNotes.setText(data[rowIndex][11] + "");
txtID.setEditable(false);
frame1.setTitle("Enter Student data");
btnAddInput.setActionCommand("Edit2");
btnAddInput.setText("ACCEPT");
frame1.setVisible(true);
} else if (e.getActionCommand().equals("DELETE")) {
int confirm = JOptionPane.showConfirmDialog(frame, "ARE YOU SURE?", "CONFIRM",
JOptionPane.YES_NO_OPTION);
if (confirm == 0) {
rowIndex = table.getSelectedRow();
rowNumber = 0;
noOfStudents--;
for (int i = 0; i <= 10; i++) {
if (rowIndex != i && i <= noOfStudents) {
data[rowNumber][0] = data[i][0];
data[rowNumber][1] = data[i][1];
data[rowNumber][2] = data[i][2];
data[rowNumber][3] = data[i][3];
data[rowNumber][4] = data[i][4];
data[rowNumber][5] = data[i][5];
data[rowNumber][6] = data[i][6];
data[rowNumber][7] = data[i][7];
data[rowNumber][8] = data[i][8];
data[rowNumber][9] = data[i][9];
data[rowNumber][10] = data[i][10];
data[rowNumber][11] = data[i][11];
rowNumber++;
} else if (rowIndex != i && i > noOfStudents) {
data[rowNumber][0] = "";
data[rowNumber][1] = "";
data[rowNumber][2] = "";
data[rowNumber][3] = "";
data[rowNumber][4] = "";
data[rowNumber][5] = "";
data[rowNumber][6] = "";
data[rowNumber][7] = "";
data[rowNumber][8] = "";
data[rowNumber][9] = "";
data[rowNumber][10] = "";
data[rowNumber][11] = "";
rowNumber++;
}
}
if (noOfStudents == 1000) {
btnAdd.setEnabled(false);
}
else {
btnAdd.setEnabled(true);
}
if (noOfStudents == 0) {
btnDelete.setEnabled(false);
btnEdit.setEnabled(false);
} else {
btnDelete.setEnabled(true);
btnEdit.setEnabled(true);
}
rowIndex = table.getSelectedRow();
if (data[rowIndex][0] == null || data[rowIndex][0] == "") {
btnEdit.setEnabled(false);
btnDelete.setEnabled(false);
} else {
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);
}
table.updateUI();
}
} else if (e.getActionCommand().equals("AddInput")) {
if (txtID.getText().isEmpty() || txtName.getText().isEmpty()
|| txtMajor.getText().isEmpty()// /
|| txtGPA.getText().isEmpty() || txtCampus.getText().isEmpty()|| txtEmail.getText().isEmpty()
|| txtCurrent.getText().isEmpty()|| txtPast.getText().isEmpty()|| txtFuture.getText().isEmpty()
|| txtNotes.getText().isEmpty() || txtPhone.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "PLEASE FILL IN THE BLANKS.", "ERROR!",
JOptionPane.ERROR_MESSAGE);
}
else {
int dup = 0;
for (int i = 0; i < 8; i++) {
if (txtID.getText().equals(data[i][0])) {
JOptionPane.showMessageDialog(null, "ID NUMBER ALREADY EXISTS.", "ERROR!",
JOptionPane.ERROR_MESSAGE);
dup++;
}
}
if (dup == 0) {
rowIndex = table.getSelectedRow();
data[noOfStudents][0] = txtID.getText();
data[noOfStudents][1] = txtName.getText();
data[noOfStudents][2] = txtMajor.getText();
data[noOfStudents][3] = txtGPA.getText();
data[noOfStudents][4] = txtCampus.getText();
data[noOfStudents][5] = txtAddress.getText();
data[noOfStudents][6] = txtPhone.getText();
data[noOfStudents][7] = txtEmail.getText();
data[noOfStudents][8] = txtCurrent.getText();
data[noOfStudents][9] = txtPast.getText();
data[noOfStudents][10] = txtFuture.getText();
data[noOfStudents][11] = txtNotes.getText();
table.updateUI();
frame1.dispose();
noOfStudents++;
if (noOfStudents == 50){
btnAdd.setEnabled(false);
}
else {
btnAdd.setEnabled(true);
}
if (data[rowIndex][0] == null) {
btnEdit.setEnabled(false);
btnDelete.setEnabled(false);
} else {
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);
}
}
}
table.updateUI();
}else if(e.getActionCommand().equals("Save")){
try {
PrintWriter out = new PrintWriter("Tutor.txt");
for(int i = 0; i < 25; i++)
{
for(int j = 0; j < 10; j++)
{
out.print(data[i][j]);
out.print(", ");
if(j == 10){
out.println();
}
} out.flush();
}} catch (FileNotFoundException ex) {
}
} else if (e.getActionCommand().equals("Update")) {
}
if (txtID.getText().isEmpty() || txtName.getText().isEmpty()
|| txtMajor.getText().isEmpty()// /
|| txtGPA.getText().isEmpty() || txtCampus.getText().isEmpty()|| txtEmail.getText().isEmpty()
|| txtCurrent.getText().isEmpty()|| txtPast.getText().isEmpty()|| txtFuture.getText().isEmpty()
|| txtNotes.getText().isEmpty()) {
} else {
data[rowIndex][0] = txtID.getText();
data[rowIndex][1] = txtName.getText();
data[rowIndex][2] = txtMajor.getText();
data[rowIndex][3] = txtGPA.getText();
data[rowIndex][4] = txtCampus.getText();
data[rowIndex][5] = txtAddress.getText();
data[rowIndex][6] = txtPhone.getText();
data[rowIndex][7] = txtEmail.getText();
data[rowIndex][8] = txtEmail.getText();
data[rowIndex][9] = txtEmail.getText();
data[rowIndex][10] = txtEmail.getText();
data[rowIndex][11] = txtEmail.getText();
frame1.dispose();
}
table.updateUI();
}
}
class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
}
@Override
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException {
if (str == null) {
return;
}
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}
public static void main(String[] args) {
new IAdvise();
}
}
学生类(class):
public class Student implements java.io.Serialize{
private final String id;
private final String name;
private final String major;
private final String gpa;
private final String campus;
private final String address;
private final String number;
private final String email;
private final String current;
private final String past;
private final String future;
public Student(String id, String name, String major,String gpa, String campus, String address,String number, String email, String current, String past, String future, String notes) {
this.id = id;
this.name = name;
this.major = major;
this.gpa = gpa;
this.campus = campus;
this.address = address;
this.number = number;
this.email = email;
this.current = current;
this.past = past;
this.future = future;
}
}
最佳答案
将使用数组的代码更改为使用 ArrayList
的代码:
而不是 a[i]
,当它不在作业的左侧时,请使用 a.get(i)
而不是 a.length
,使用a.size()
而不是设置 a[i] = expression
:如果i
已知在范围内 (0 <= i
<= a.length-1
),请使用 a.set(i, expression)
。如果i == a.length
,您不能使用a[i] = expression
使用数组,但您可以使用 ArrayList
来完成,将大小增加 1: a.add(expression)
。如果i > a.length
,以便您想要在 ArrayList
中添加一个新元素留下一个间隙,然后是这样的:
while (a.size() < i)
a.add (null);
// at this point a.size == i
a.add (expression);
// now a.size == i+1
这应该涵盖数组上的所有基本操作。 Arrays
中定义了一些实用程序操作。 ArrayList
中没有等效操作,例如binarySearch
.
关于java - 如何从 Array 切换到 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20623209/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!