How would I switch from an Array to an ArrayList?
本问题已经有最佳答案,请猛点这里访问。
我现在把所有的数据保存到一个数组中。我如何将它切换到保存在对象数组列表中?我已经编写了一个基本的学生对象类,但需要帮助将其转换为数组列表中的保存。非常感谢您的帮助。arraylist在第28行创建,数据设置在第300行。问题是如何修改代码以存储在arraylist中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | 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(); } } |
学生班:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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; } } |
要将使用数组的代码更改为使用
当它不在任务的左侧时,使用
用
不设置
1 2 3 4 5 | while (a.size() < i) a.add (null); // at this point a.size == i a.add (expression); // now a.size == i+1 |
这应该包括对数组的所有基本操作。在