Confuzzled in sorting arraylist...

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 23
Reputation: Evil_genius82 is an unknown quantity at this point 
Solved Threads: 0
Evil_genius82 Evil_genius82 is offline Offline
Newbie Poster

Confuzzled in sorting arraylist...

 
0
  #1
Jul 25th, 2009
hey guys, im very new to java so bare with me...

i cant seem to bubble sort my arraylist...
The data needs to be displayed unsorted in a jlist, then when user clicks process the list is sorted according to student id.
If anyone could guide me through it would be great because ive been stuck on it for days....

i dont know how much code you need but im going to post all of it & if you need anymore details please let me know.

Thanks to anyone in advance...

  1.  
  2. package assignment2009;
  3.  
  4. import java.awt.event.MouseEvent;
  5. import javax.swing.*;
  6. import java.awt.event.MouseListener;
  7. import java.util.*;
  8.  
  9.  
  10.  
  11. public class MainApplication extends javax.swing.JFrame implements MouseListener {
  12.  
  13.  
  14. public MainApplication()
  15. {
  16. initComponents();
  17. tfJList.addMouseListener(this);
  18.  
  19. }
  20.  
  21. @SuppressWarnings("unchecked")
  22.  
  23. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  24. private void initComponents() {
  25.  
  26. top1Panel = new javax.swing.JPanel();
  27. clearTFButton = new javax.swing.JButton();
  28. jScrollPane1 = new javax.swing.JScrollPane();
  29. tfJList = new javax.swing.JList();
  30. top2Panel = new javax.swing.JPanel();
  31. jScrollPane2 = new javax.swing.JScrollPane();
  32. newMFile = new javax.swing.JList();
  33. infoBarButton = new javax.swing.JLabel();
  34. top3Panel = new javax.swing.JPanel();
  35. clearMFButton = new javax.swing.JButton();
  36. jScrollPane3 = new javax.swing.JScrollPane();
  37. mfJList = new javax.swing.JList();
  38. top4Panel = new javax.swing.JPanel();
  39. jScrollPane4 = new javax.swing.JScrollPane();
  40. errorLog = new javax.swing.JTextArea();
  41. elLabel = new javax.swing.JLabel();
  42. botton1Panel = new javax.swing.JPanel();
  43. etButton = new javax.swing.JLabel();
  44. blank = new javax.swing.JLabel();
  45. jLabel2 = new javax.swing.JLabel();
  46. nameSur = new javax.swing.JTextField();
  47. jLabel1 = new javax.swing.JLabel();
  48. iD = new javax.swing.JTextField();
  49. previousButton = new javax.swing.JButton();
  50. nextRecord = new javax.swing.JButton();
  51. deleteButton = new javax.swing.JButton();
  52. addButton = new javax.swing.JButton();
  53. modifyButton = new javax.swing.JButton();
  54. defaultRecordsButton = new javax.swing.JButton();
  55. bottom2Panel = new javax.swing.JPanel();
  56. soLabel = new javax.swing.JLabel();
  57. jLabel4 = new javax.swing.JLabel();
  58. demoSpeedSlider = new javax.swing.JSlider();
  59. jLabel3 = new javax.swing.JLabel();
  60. jLabel5 = new javax.swing.JLabel();
  61. singleShotCB = new javax.swing.JCheckBox();
  62. bottom3Panel = new javax.swing.JPanel();
  63. dcLabel = new javax.swing.JLabel();
  64. sDemoButton = new javax.swing.JButton();
  65. pDemoButton = new javax.swing.JButton();
  66. jToggleButton1 = new javax.swing.JToggleButton();
  67. bottom4Panel = new javax.swing.JPanel();
  68. jScrollPane5 = new javax.swing.JScrollPane();
  69. helpTArea = new javax.swing.JTextArea();
  70. helpButton = new javax.swing.JLabel();
  71.  
  72. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  73. getContentPane().setLayout(new java.awt.GridLayout(2, 5));
  74.  
  75. top1Panel.setLayout(new java.awt.BorderLayout());
  76.  
  77. clearTFButton.setText("Clear Transaction File");
  78. clearTFButton.addActionListener(new java.awt.event.ActionListener() {
  79. public void actionPerformed(java.awt.event.ActionEvent evt) {
  80. clearTFButtonActionPerformed(evt);
  81. }
  82. });
  83. top1Panel.add(clearTFButton, java.awt.BorderLayout.PAGE_END);
  84.  
  85. tfJList.setBackground(new java.awt.Color(0, 204, 255));
  86. tfJList.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2), "Transaction File", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Impact", 0, 13), new java.awt.Color(0, 0, 0))); // NOI18N
  87. tfJList.setModel(tranList);
  88. tfJList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
  89. jScrollPane1.setViewportView(tfJList);
  90.  
  91. top1Panel.add(jScrollPane1, java.awt.BorderLayout.CENTER);
  92.  
  93. getContentPane().add(top1Panel);
  94.  
  95. top2Panel.setLayout(new java.awt.BorderLayout());
  96.  
  97. newMFile.setBackground(new java.awt.Color(255, 51, 51));
  98. newMFile.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2), "New Master File", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Impact", 0, 13), new java.awt.Color(0, 0, 0))); // NOI18N
  99. newMFile.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
  100. jScrollPane2.setViewportView(newMFile);
  101.  
  102. top2Panel.add(jScrollPane2, java.awt.BorderLayout.CENTER);
  103.  
  104. infoBarButton.setBackground(new java.awt.Color(102, 255, 51));
  105. infoBarButton.setFont(new java.awt.Font("Tahoma", 2, 14));
  106. infoBarButton.setForeground(new java.awt.Color(0, 51, 255));
  107. infoBarButton.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  108. infoBarButton.setText("Info Bar");
  109. infoBarButton.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 3, true));
  110. top2Panel.add(infoBarButton, java.awt.BorderLayout.PAGE_END);
  111.  
  112. getContentPane().add(top2Panel);
  113.  
  114. top3Panel.setLayout(new java.awt.BorderLayout());
  115.  
  116. clearMFButton.setText("Clear Master File");
  117. clearMFButton.addActionListener(new java.awt.event.ActionListener() {
  118. public void actionPerformed(java.awt.event.ActionEvent evt) {
  119. clearMFButtonActionPerformed(evt);
  120. }
  121. });
  122. top3Panel.add(clearMFButton, java.awt.BorderLayout.PAGE_END);
  123.  
  124. mfJList.setBackground(new java.awt.Color(102, 255, 102));
  125. mfJList.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2), "Master File", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Impact", 0, 13), new java.awt.Color(0, 0, 0))); // NOI18N
  126. mfJList.setModel(masterList);
  127. mfJList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
  128. jScrollPane3.setViewportView(mfJList);
  129.  
  130. top3Panel.add(jScrollPane3, java.awt.BorderLayout.CENTER);
  131.  
  132. getContentPane().add(top3Panel);
  133.  
  134. top4Panel.setLayout(new java.awt.BorderLayout());
  135.  
  136. errorLog.setBackground(new java.awt.Color(0, 102, 204));
  137. errorLog.setColumns(20);
  138. errorLog.setRows(5);
  139. jScrollPane4.setViewportView(errorLog);
  140.  
  141. top4Panel.add(jScrollPane4, java.awt.BorderLayout.CENTER);
  142.  
  143. elLabel.setFont(new java.awt.Font("Impact", 0, 13));
  144. elLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  145. elLabel.setText("Error Log");
  146. top4Panel.add(elLabel, java.awt.BorderLayout.PAGE_START);
  147.  
  148. getContentPane().add(top4Panel);
  149.  
  150. botton1Panel.setLayout(new java.awt.GridLayout(7, 2, 10, 10));
  151.  
  152. etButton.setFont(new java.awt.Font("Impact", 0, 13));
  153. etButton.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  154. etButton.setText("Records");
  155. botton1Panel.add(etButton);
  156. botton1Panel.add(blank);
  157.  
  158. jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
  159. jLabel2.setText("Name / Surname:");
  160. botton1Panel.add(jLabel2);
  161.  
  162. nameSur.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  163. nameSur.addActionListener(new java.awt.event.ActionListener() {
  164. public void actionPerformed(java.awt.event.ActionEvent evt) {
  165. nameSurActionPerformed(evt);
  166. }
  167. });
  168. botton1Panel.add(nameSur);
  169.  
  170. jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
  171. jLabel1.setText("ID Number:");
  172. botton1Panel.add(jLabel1);
  173.  
  174. iD.addActionListener(new java.awt.event.ActionListener() {
  175. public void actionPerformed(java.awt.event.ActionEvent evt) {
  176. iDActionPerformed(evt);
  177. }
  178. });
  179. botton1Panel.add(iD);
  180.  
  181. previousButton.setText("< Previous Record");
  182. previousButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  183. previousButton.addActionListener(new java.awt.event.ActionListener() {
  184. public void actionPerformed(java.awt.event.ActionEvent evt) {
  185. previousButtonActionPerformed(evt);
  186. }
  187. });
  188. botton1Panel.add(previousButton);
  189.  
  190. nextRecord.setText("Next Record >");
  191. nextRecord.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  192. nextRecord.addActionListener(new java.awt.event.ActionListener() {
  193. public void actionPerformed(java.awt.event.ActionEvent evt) {
  194. nextRecordActionPerformed(evt);
  195. }
  196. });
  197. botton1Panel.add(nextRecord);
  198.  
  199. deleteButton.setText("Delete Record");
  200. deleteButton.addActionListener(new java.awt.event.ActionListener() {
  201. public void actionPerformed(java.awt.event.ActionEvent evt) {
  202. deleteButtonActionPerformed(evt);
  203. }
  204. });
  205. botton1Panel.add(deleteButton);
  206.  
  207. addButton.setText("Add Record");
  208. addButton.addActionListener(new java.awt.event.ActionListener() {
  209. public void actionPerformed(java.awt.event.ActionEvent evt) {
  210. addButtonActionPerformed(evt);
  211. }
  212. });
  213. botton1Panel.add(addButton);
  214.  
  215. modifyButton.setText("Modify Record");
  216. modifyButton.addActionListener(new java.awt.event.ActionListener() {
  217. public void actionPerformed(java.awt.event.ActionEvent evt) {
  218. modifyButtonActionPerformed(evt);
  219. }
  220. });
  221. botton1Panel.add(modifyButton);
  222.  
  223. defaultRecordsButton.setText("Load Default Records");
  224. defaultRecordsButton.addActionListener(new java.awt.event.ActionListener() {
  225. public void actionPerformed(java.awt.event.ActionEvent evt) {
  226. defaultRecordsButtonActionPerformed(evt);
  227. }
  228. });
  229. botton1Panel.add(defaultRecordsButton);
  230.  
  231. getContentPane().add(botton1Panel);
  232.  
  233. bottom2Panel.setLayout(new java.awt.GridLayout(7, 1, 10, 10));
  234.  
  235. soLabel.setFont(new java.awt.Font("Impact", 0, 13));
  236. soLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  237. soLabel.setText("Simulation Options");
  238. bottom2Panel.add(soLabel);
  239.  
  240. jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  241. jLabel4.setText("--- Automatic Simulation ---");
  242. bottom2Panel.add(jLabel4);
  243.  
  244. demoSpeedSlider.setMaximum(10);
  245. demoSpeedSlider.setMinimum(1);
  246. demoSpeedSlider.setMinorTickSpacing(1);
  247. demoSpeedSlider.setPaintLabels(true);
  248. demoSpeedSlider.setPaintTicks(true);
  249. demoSpeedSlider.setSnapToTicks(true);
  250. demoSpeedSlider.setValue(5);
  251. bottom2Panel.add(demoSpeedSlider);
  252.  
  253. jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  254. jLabel3.setText("Slow Speed Fast");
  255. jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  256. bottom2Panel.add(jLabel3);
  257.  
  258. jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  259. jLabel5.setText("--- Manual Simulation ---");
  260. bottom2Panel.add(jLabel5);
  261.  
  262. singleShotCB.setText("Single Shot");
  263. singleShotCB.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  264. bottom2Panel.add(singleShotCB);
  265.  
  266. getContentPane().add(bottom2Panel);
  267.  
  268. bottom3Panel.setLayout(new java.awt.GridLayout(7, 1, 10, 10));
  269.  
  270. dcLabel.setFont(new java.awt.Font("Impact", 0, 13));
  271. dcLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  272. dcLabel.setText("Demonstration Controls");
  273. bottom3Panel.add(dcLabel);
  274.  
  275. sDemoButton.setText("Start Demonstration");
  276. sDemoButton.addActionListener(new java.awt.event.ActionListener() {
  277. public void actionPerformed(java.awt.event.ActionEvent evt) {
  278. sDemoButtonActionPerformed(evt);
  279. }
  280. });
  281. bottom3Panel.add(sDemoButton);
  282.  
  283. pDemoButton.setText("Pause");
  284. pDemoButton.addActionListener(new java.awt.event.ActionListener() {
  285. public void actionPerformed(java.awt.event.ActionEvent evt) {
  286. pDemoButtonActionPerformed(evt);
  287. }
  288. });
  289. bottom3Panel.add(pDemoButton);
  290.  
  291. jToggleButton1.setText("Next Step >");
  292. bottom3Panel.add(jToggleButton1);
  293.  
  294. getContentPane().add(bottom3Panel);
  295.  
  296. bottom4Panel.setLayout(new java.awt.BorderLayout());
  297.  
  298. helpTArea.setBackground(new java.awt.Color(255, 255, 51));
  299. helpTArea.setColumns(20);
  300. helpTArea.setEditable(false);
  301. helpTArea.setFont(new java.awt.Font("Monospaced", 0, 12));
  302. helpTArea.setLineWrap(true);
  303. helpTArea.setRows(5);
  304. helpTArea.setText("Step 1\nAdd records to transaction file.\n\nStep 2\nSelect speed of demonstration or select single shot to process data in an instant.\n\nStep 3\nSelect Start Demonstation!");
  305. helpTArea.setWrapStyleWord(true);
  306. jScrollPane5.setViewportView(helpTArea);
  307.  
  308. bottom4Panel.add(jScrollPane5, java.awt.BorderLayout.CENTER);
  309.  
  310. helpButton.setFont(new java.awt.Font("Impact", 0, 13));
  311. helpButton.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  312. helpButton.setText("Help ");
  313. bottom4Panel.add(helpButton, java.awt.BorderLayout.PAGE_START);
  314.  
  315. getContentPane().add(bottom4Panel);
  316.  
  317. pack();
  318. }// </editor-fold>
  319.  
  320. private void clearTFButtonActionPerformed(java.awt.event.ActionEvent evt) {
  321. tranList.clear();
  322. infoBarButton.setText("Transaction File Cleared");
  323. }
  324.  
  325. private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
  326. addRecord();
  327. }
  328.  
  329. private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {
  330. modify();
  331. }
  332.  
  333. private void defaultRecordsButtonActionPerformed(java.awt.event.ActionEvent evt) {
  334. addDefaultData();
  335. }
  336.  
  337. private void sDemoButtonActionPerformed(java.awt.event.ActionEvent evt) {
  338. sort.bubbleSort(studTranArray );
  339. }
  340.  
  341. private void pDemoButtonActionPerformed(java.awt.event.ActionEvent evt) {
  342. // PAUSE
  343. }
  344.  
  345. private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
  346. delete();
  347. }
  348.  
  349. private void iDActionPerformed(java.awt.event.ActionEvent evt) {
  350. // TODO add your handling code here:
  351. }
  352.  
  353. private void clearMFButtonActionPerformed(java.awt.event.ActionEvent evt) {
  354. masterList.clear();
  355. infoBarButton.setText("Master File Cleared");
  356. }
  357.  
  358. private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {
  359. previousRecord();
  360. }
  361.  
  362. private void nextRecordActionPerformed(java.awt.event.ActionEvent evt) {
  363. System.out.println(studTranArray.size());
  364. nextRecord();
  365. }
  366.  
  367. private void nameSurActionPerformed(java.awt.event.ActionEvent evt) {
  368. // TODO add your handling code here:
  369. }
  370.  
  371.  
  372. public static void main(String args[]) {
  373. java.awt.EventQueue.invokeLater(new Runnable() {
  374. public void run() {
  375. new MainApplication().setVisible(true);
  376.  
  377.  
  378. }
  379. });
  380. }
  381.  
  382.  
  383. //javax.swing.tfJList=newList;
  384. private DefaultListModel tranList = new DefaultListModel();
  385. private DefaultListModel masterList = new DefaultListModel();
  386. private DefaultListModel newMasterList = new DefaultListModel();
  387. studentClass Nstudent = new studentClass();
  388.  
  389. //Default Transaction Array
  390. ArrayList studTranArray = new ArrayList();
  391. Vector studentVector = new Vector(studTranArray);
  392.  
  393. //Default Master File Array
  394. List defNameMasterList = new ArrayList(); //Creates List Objects for default
  395.  
  396.  
  397. //Default NewMaster File Array
  398. List defNameNewMasterList = new ArrayList();
  399.  
  400. int nOrecords=0;
  401. int index = 0;
  402.  
  403.  
  404. // Variables declaration - do not modify
  405. private javax.swing.JButton addButton;
  406. private javax.swing.JLabel blank;
  407. private javax.swing.JPanel bottom2Panel;
  408. private javax.swing.JPanel bottom3Panel;
  409. private javax.swing.JPanel bottom4Panel;
  410. private javax.swing.JPanel botton1Panel;
  411. private javax.swing.JButton clearMFButton;
  412. private javax.swing.JButton clearTFButton;
  413. private javax.swing.JLabel dcLabel;
  414. private javax.swing.JButton defaultRecordsButton;
  415. private javax.swing.JButton deleteButton;
  416. private javax.swing.JSlider demoSpeedSlider;
  417. private javax.swing.JLabel elLabel;
  418. private javax.swing.JTextArea errorLog;
  419. private javax.swing.JLabel etButton;
  420. private javax.swing.JLabel helpButton;
  421. private javax.swing.JTextArea helpTArea;
  422. private javax.swing.JTextField iD;
  423. private javax.swing.JLabel infoBarButton;
  424. private javax.swing.JLabel jLabel1;
  425. private javax.swing.JLabel jLabel2;
  426. private javax.swing.JLabel jLabel3;
  427. private javax.swing.JLabel jLabel4;
  428. private javax.swing.JLabel jLabel5;
  429. private javax.swing.JScrollPane jScrollPane1;
  430. private javax.swing.JScrollPane jScrollPane2;
  431. private javax.swing.JScrollPane jScrollPane3;
  432. private javax.swing.JScrollPane jScrollPane4;
  433. private javax.swing.JScrollPane jScrollPane5;
  434. private javax.swing.JToggleButton jToggleButton1;
  435. private javax.swing.JList mfJList;
  436. private javax.swing.JButton modifyButton;
  437. private javax.swing.JTextField nameSur;
  438. private javax.swing.JList newMFile;
  439. private javax.swing.JButton nextRecord;
  440. private javax.swing.JButton pDemoButton;
  441. private javax.swing.JButton previousButton;
  442. private javax.swing.JButton sDemoButton;
  443. private javax.swing.JCheckBox singleShotCB;
  444. private javax.swing.JLabel soLabel;
  445. private javax.swing.JList tfJList;
  446. private javax.swing.JPanel top1Panel;
  447. private javax.swing.JPanel top2Panel;
  448. private javax.swing.JPanel top3Panel;
  449. private javax.swing.JPanel top4Panel;
  450. // End of variables declaration
  451. //-----------------------------------------------------------------
  452. public void addRecord()
  453. {
  454. studTranArray.add(iD.getText());
  455. studTranArray.add(nameSur.getText());
  456.  
  457. tranList.addElement(nameSur.getText()+" "+iD.getText());
  458. clear();
  459. infoBarButton.setText("Record Added To Transaction File!");
  460. }
  461. //------------------------------------------------------------------
  462. public void clear()
  463. {
  464. nameSur.setText("");
  465. iD.setText("");
  466.  
  467. }
  468. //------------------------------------------------------------------
  469. public void addDefaultData()
  470. {
  471. String [] defaultNames = {"Alvin Du","Ryan Gosden","Michelle Dowling","Toni Dowling"};
  472. int [] defaultID = {153679,417950,234512,762903};
  473.  
  474.  
  475. for (int i=0;i<4;i++)
  476. {
  477. Nstudent.setStudentName(defaultNames[i]);
  478. Nstudent.setStudentID(defaultID[i]);
  479.  
  480. studTranArray.add(Nstudent);
  481.  
  482. tranList.addElement(defaultNames[i]+" "+defaultID[i]); //adds arrays to JList
  483. }
  484.  
  485. infoBarButton.setText("Default Records Loaded!");
  486.  
  487. }
  488. //------------------------------------------------------------------
  489. public void delete()
  490. {
  491.  
  492. System.out.println(index);
  493. if (index ==0 || index<0)
  494. {
  495. JOptionPane.showMessageDialog(null,"No More Records To Delete");
  496. }
  497. else
  498. {
  499. tranList.remove(index);
  500. studTranArray.remove(index);
  501. }
  502. }
  503.  
  504. //------------------------------------------------------------------
  505. public void nextRecord()
  506. {
  507. int size =(studTranArray.size());
  508.  
  509. if (nOrecords<size)
  510. {
  511. iD.setText(studTranArray.get(nOrecords).toString());
  512. nOrecords = nOrecords +1;
  513. }
  514. else
  515. {
  516. JOptionPane.showMessageDialog(null,"No More Records");
  517. }
  518. }
  519. //------------------------------------------------------------------
  520. public void previousRecord()
  521. {
  522. if(nOrecords>0)
  523. {
  524. nOrecords = nOrecords -1;
  525. iD.setText(studTranArray.get(nOrecords).toString());
  526. }
  527. else
  528. {
  529. JOptionPane.showMessageDialog(null,"No More Records");
  530. }
  531. }
  532. //------------------------------------------------------------------
  533. public void modify()
  534. {
  535. tranList.remove(index);
  536. studTranArray.remove(index);
  537. studTranArray.add(iD.getText());
  538. tranList.addElement(iD.getText());
  539. }
  540. //------------------------------------------------------------------
  541. //Sort Method!!!!!!
  542. public static void sort(ArrayList sortedList)
  543. {
  544.  
  545. int count = 1;
  546. int size2 =(sortedList.size());
  547.  
  548. for (int i = 0; i < size2; i++)
  549. {
  550. System.out.print(sortedList.get(i) +" ");
  551. }
  552.  
  553.  
  554. if (count % 10 == 0)
  555. {
  556. System.out.println();
  557. }
  558. count = count + 1;
  559. }
  560.  
  561. static int bubbleSort(ArrayList <Integer> sortedList)
  562. {
  563. int count = 0;
  564. for (int outer = 0; outer < sortedList.size() - 1; outer++)
  565. {
  566. for (int inner = 0; inner < sortedList.size()-outer-1; inner++)
  567. {
  568. if (sortedList.get(inner) > sortedList.get(inner + 1))
  569. {
  570. swapEm(sortedList, inner);
  571. count = count + 1;
  572. }
  573. }
  574. }
  575. return count;
  576. }
  577. static void swapEm(ArrayList<Integer>sortedList, int inner)
  578. {
  579. int temp = sortedList.get(inner);
  580. sortedList.set(inner, sortedList.get(inner + 1));
  581. sortedList.set(inner + 1, temp);
  582. }
  583.  
  584.  
  585. //------------------------------------------------------------------
  586. public void mouseClicked(MouseEvent e)
  587. {
  588.  
  589. JList tranList = (JList)e.getSource();
  590. if (e.getClickCount() == 1)
  591. {
  592. index = tranList.locationToIndex(e.getPoint());
  593. iD.setText(studTranArray.get(index).toString());
  594. nameSur.setText(studTranArray.get(index).toString());
  595.  
  596. infoBarButton.setText("Item Selected");
  597. }
  598. }
  599. public void mouseEntered(MouseEvent e)
  600. {
  601.  
  602. }
  603. public void mouseExited(MouseEvent e)
  604. {
  605.  
  606. }
  607. public void mousePressed(MouseEvent e)
  608. {
  609.  
  610. }
  611. public void mouseReleased(MouseEvent e)
  612. {
  613.  
  614. }
  615. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,241
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 491
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Confuzzled in sorting arraylist...

 
-1
  #2
Jul 25th, 2009
Have look at this section from SCJP study guide
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 23
Reputation: Evil_genius82 is an unknown quantity at this point 
Solved Threads: 0
Evil_genius82 Evil_genius82 is offline Offline
Newbie Poster

Re: Confuzzled in sorting arraylist...

 
0
  #3
Jul 25th, 2009
Ok thanks Peter
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 23
Reputation: Evil_genius82 is an unknown quantity at this point 
Solved Threads: 0
Evil_genius82 Evil_genius82 is offline Offline
Newbie Poster

Re: Confuzzled in sorting arraylist...

 
0
  #4
Jul 25th, 2009
I tried Collections.sort but i get this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: assignment2009.studentClass cannot be cast to java.lang.Comparable

The arraylist i want to sort (studTranArray) contains a string and an integer.
While the example only shows a string being sorted...
is there a way to select the variable type you want for sorting and the String will follow?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,241
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 491
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Confuzzled in sorting arraylist...

 
-1
  #5
Jul 25th, 2009
Can you post your updated code please, so we see what you tried and where can be problem?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster

Re: Confuzzled in sorting arraylist...

 
0
  #6
Jul 26th, 2009
Sorry about the code formatting. You have multiple things going on:

In "addDefaultData" you give the impression that "studTranArray" is a record.
Ok, records/struct don't really exist in Java so you can use a class to
try to achieve something similiar.

You do the following:

String [] defaultNames = {"Alvin Du","Ryan Gosden","Michelle Dowling","Toni Dowling"};
int [] defaultID = {153679,417950,234512,762903};

Nstudent.setStudentName(defaultNames[i]);
Nstudent.setStudentID(defaultID[i]);

studTranArray.add(Nstudent);


------------------------------------------------------------------------------
You declare "studTranArray" as follows:
ArrayList studTranArray = new ArrayList();

----------------------------------------------------

then in "addRecord", you do the following:
studTranArray.add(iD.getText());
studTranArray.add(nameSur.getText());

you are mixing what should probably be integer data (ID) with
String data (name). You need to keep your id's separate from
the names. Looks like you started to do this in "addDefaultData".
----------------------------------------------------
in "sDemoButtonActionPerformed" you call:
sort.bubbleSort(studTranArray );

but you declare "bubbleSort" as follows:

static int bubbleSort(ArrayList <Integer> sortedList)

------------------------------------------------------
Looks like you added "String" data to the array list and told
bubblesSort that you were going to pass it an "Integer" array list.

Even if you remove "<Integer>" from your bubbleSort declaration,
you can't compare String data with ">" or "<". You have to use
"compareTo" or "compareToIgnoreCase".

-------------------------------------------------------
I would recommend creating a separate class that separates the student data:

  1.  
  2. package assignment2009;
  3.  
  4. public class StudentClass {
  5.  
  6. public StudentClass() {
  7. }
  8.  
  9.  
  10. protected String studentName;
  11. protected int studentID;
  12.  
  13.  
  14.  
  15. public void setStudentName(String newStudentName)
  16. {
  17. this.studentName = newStudentName;
  18. }
  19.  
  20. public String getStudentName()
  21. {
  22. return studentName;
  23. }
  24.  
  25. public void setStudentID(int newStudentID)
  26. {
  27. this.studentID = newStudentID;
  28. }
  29.  
  30. public int getStudentID()
  31. {
  32. return studentID;
  33. }
  34.  
  35. }

Then in the main application have something like the following:

  1. package assignment2009;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Vector;
  5. import javax.swing.JOptionPane;
  6.  
  7. public class MainApplication extends javax.swing.JFrame {
  8.  
  9.  
  10. //create array list of type StudentClass
  11. ArrayList<StudentClass> studTranArray = new ArrayList<StudentClass>();
  12.  
  13. //used to keep track of record number when pressing "previous" or "next"
  14. int recordNumber = 0;
  15.  
  16. //used to keep track of how many array elements/students we have
  17. int recordCount = 0;
  18.  
  19.  
  20.  
  21. //more code goes here
  22.  
  23. //------------------------------------------------------------------
  24. public void addDefaultData() {
  25.  
  26. //may need to change some of the data around for testing
  27. //"order by name" and "order by id" results in students being in the exact same order
  28.  
  29. String[] defaultNames = {"Alvin Du", "Ryan Gosden", "Michelle Dowling", "Toni Dowling"};
  30. int[] defaultID = {153679, 417950, 234512, 762903};
  31.  
  32. //clear array of any previous data
  33. studTranArray.clear();
  34.  
  35. for (int i = 0; i < defaultNames.length; i++) {
  36. recordCount = recordCount + 1;
  37.  
  38. //need to create new Nstudent for each loop
  39. //otherwise all data will = the last student entered
  40.  
  41. StudentClass Nstudent = new StudentClass();
  42.  
  43. Nstudent.setStudentID(defaultID[i]);
  44. Nstudent.setStudentName(defaultNames[i]);
  45.  
  46. studTranArray.add(Nstudent);
  47. }
  48.  
  49. //displayData(studTranArray);
  50. //statusLbl.setText("Default Records Loaded!");
  51.  
  52. }
  53.  
  54. //------------------------------------------------------------------
  55.  
  56. private void bubbleSort(ArrayList<StudentClass> sortedList, String sortBy)
  57. {
  58. //start with the first element in the array
  59. for (int outer = 0; outer < sortedList.size() - 1; outer++)
  60. {
  61. //compare "outer" element with rest of elements one by one
  62. //swap if necessary
  63. for (int inner = outer+1; inner < sortedList.size(); inner++)
  64. {
  65. //if sorting by id compare ids
  66. if (sortBy.equalsIgnoreCase("id"))
  67. {
  68. if (sortedList.get(outer).getStudentID() > sortedList.get(inner).getStudentID())
  69. {
  70. statusLbl.setText("Swapping id:" + sortedList.get(inner).getStudentID() + " with " + sortedList.get(outer).getStudentID());
  71.  
  72. swapEm(sortedList, outer, inner);
  73. //displayData(sortedList);
  74. }//end if
  75. }
  76. else if(sortBy.equalsIgnoreCase("name"))
  77. {
  78. //use compareToIgnoreCase for String comparison
  79. int result = sortedList.get(outer).getStudentName().compareToIgnoreCase (sortedList.get(inner).getStudentName());
  80.  
  81. //if "outer name" is alphabetically after "inner name" swap them
  82. if (result > 0)
  83. {
  84. statusLbl.setText("Swapping id:" + sortedList.get(inner).getStudentNam()
  85. + " with " + sortedList.get(outer).getStudentName());
  86.  
  87. swapEm(sortedList, outer, inner);
  88. //displayData(sortedList);
  89. }//end if
  90. }//end else if
  91. }//end for
  92. }//end for
  93. }
  94.  
  95.  
  96. private void swapEm(ArrayList<StudentClass> sortedList, int outer, int inner)
  97. {
  98.  
  99. StudentClass temp = sortedList.get(inner);
  100. sortedList.set(inner, sortedList.get(outer));
  101. sortedList.set(outer, temp);
  102. }
  103.  
  104. }//end MainApplication
Last edited by cgeier; Jul 26th, 2009 at 3:38 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 471
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Confuzzled in sorting arraylist...

 
0
  #7
Jul 26th, 2009
Originally Posted by Evil_genius82 View Post
I tried Collections.sort but i get this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: assignment2009.studentClass cannot be cast to java.lang.Comparable

The arraylist i want to sort (studTranArray) contains a string and an integer.
While the example only shows a string being sorted...
is there a way to select the variable type you want for sorting and the String will follow?
The runtime exception is thrown because the `sort' methods expects the ArrayList elements to implement the Comparable interface i.e. the class should have a method which logically compares students. If you can't change your class definition [for e.g. if your value objects or classes are automatically generated], you can use a variation of the `sort' method which accepts a custom Comparator.
  1. // NULL checks and some other minor details omitted for brevity
  2. class Student implements Comparable<Student> {
  3.  
  4. private String name;
  5.  
  6. private int id;
  7.  
  8. public Student(String name, int id) {
  9. this.id = id;
  10. this.name = name;
  11. }
  12.  
  13. public String getName() {
  14. return this.name;
  15. }
  16.  
  17. public int compareTo(Student student) {
  18. return this.id - student.id;
  19. }
  20.  
  21. @Override
  22. public String toString() {
  23. return "{name:" + name + ",id:" + id + "}";
  24. }
  25.  
  26. }
  27.  
  28. public class ProxyTest {
  29.  
  30. public static void main(final String[] args) {
  31. List<Student> list = new ArrayList<Student>();
  32. Student student = new Student("tom", 12);
  33. list.add(student);
  34. student = new Student("dick", 34);
  35. list.add(student);
  36. student = new Student("harry", 0);
  37. list.add(student);
  38. student = new Student("a sexy girl", -12);
  39. list.add(student);
  40.  
  41. // Use the comparable nature of Student class when sorting i.e. sort by id
  42. Collections.sort(list);
  43. System.out.println(list);
  44.  
  45. // Use a custom compare method which now sorts by name
  46. // Here we create an instance of anonymous class which implements the
  47. // Comarator interface. In case you need to re-use the same Comparator
  48. // at multiple places, create a proper class with the same code.
  49. Collections.sort(list, new Comparator<Student>() {
  50. public int compare(Student o1, Student o2) {
  51. return o1.getName().compareTo(o2.getName());
  52. }
  53. });
  54. System.out.println(list);
  55. }
  56.  
  57. }
Originally Posted by output
[{name:a sexy girl,id:-12}, {name:harry,id:0}, {name:tom,id:12}, {name:dick,id:34}]
[{name:a sexy girl,id:-12}, {name:dick,id:34}, {name:harry,id:0}, {name:tom,id:12}]
As you can see, by crafting out well thought out compare methods, I've managed to keep "a sexy girl" ahead of others. :-)
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 23
Reputation: Evil_genius82 is an unknown quantity at this point 
Solved Threads: 0
Evil_genius82 Evil_genius82 is offline Offline
Newbie Poster

Re: Confuzzled in sorting arraylist...

 
0
  #8
Jul 26th, 2009
It seems like i cant use Collections.sort because one of the main functionalities of this program is when the list gets sorted, the user has control over the speed of EACH record from arraylist being sorted into a new jlist....

Sorry if that sounds confusing...
here is an example

arraylist data unsorted on jlist A
George 34567
Peter 23456
Sam 12345

User now selects speed of items added to jlist (ie 5 seconds)

arraylist data sorted on jlist B
George 12345 (every 5 seconds a name enters the list)
Peter 23456
Sam 34567

So far i cant seem to get the bubblesort method in my add to system.out.println the sorted data.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster

Re: Confuzzled in sorting arraylist...

 
0
  #9
Jul 26th, 2009
Please post more information about the requirements for your program. Also more about how the data should look during different stages of the program execution as well as explanations as to why the data should look that way. It'd also be nice if you could show how the data is supposed to look (or how you think it looks) inside the arraylist (including data types).
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 23
Reputation: Evil_genius82 is an unknown quantity at this point 
Solved Threads: 0
Evil_genius82 Evil_genius82 is offline Offline
Newbie Poster

Re: Confuzzled in sorting arraylist...

 
0
  #10
Jul 27th, 2009
Thanks cgeier & s.o.s for your help. I understand what your getting it and ill try out. its taking a bit of time to understand the full concept of OOP. Thanks for having patience...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC