Successfully reported this slideshow.

Oop lecture8

376 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Oop lecture8

  1. 1. Lecture 8More on collections Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. LinkedList LinkedList is just like ArrayList; however, it was designed to make add/remove faster and efficient Has these methods: addFirst AddLast removeFirst removeLast
  3. 3. Who implements List interface ArrayList LinkedList Vector Stack
  4. 4. Sets Examples are HashSet, TreeSet Difference between Set and List A list can have duplicate objects Set cannot have duplicate objects See example in the next slide
  5. 5. Output size of set: 4 0 1 2 3 size of list: 5 0 1 2 3 3We can see set did not add duplicate element
  6. 6. public class SetDemo { public static void main(String[] args) { Set<Integer>set = new HashSet<Integer>(); List<Integer>list = new ArrayList<Integer>(); for (int i=0; i<4; i++){ set.add(new Integer(i)); list.add(new Integer(i)); } set.add(3); list.add(3); System.out.println("size of set: " + set.size()); for (int i=0; i<set.size(); i++) System.out.println(set.toArray()[i]); System.out.println("size of list: " + list.size()); for (int i=0; i<list.size(); i++) System.out.println(list.get(i)); } }Output in the following slide
  7. 7. Map interface Object to a map are key,value pairs For example,<dhaka, 02><chittagong, 031><sylhet, 032> Two most common map implementations are HashMap and TreeMap
  8. 8. HashMap examplepublic class MapDemo { public static void main(String[] args) { Map<String, Integer>map = new HashMap<String, Integer>(); map.put("Dhaka", 2); map.put("Chittagong", 31); map.put("Sylhet", 32); System.out.println(map.get("Chittagong")); System.out.println(map.get("Comilla")); }} Output is: 31 null
  9. 9. Inner classesNext slide: a demo without using inner class
  10. 10. public class ButtonListenerDemo { public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.setLayout(new FlowLayout()); JButton jbutton = new JButton("Test button"); jframe.add(jbutton); jbutton.addActionListener(new ButtonListener()); JTextField jtext = new JTextField(); jframe.getContentPane().add(jtext); jtext.setText("Hello"); jframe.setVisible(true); }}class ButtonListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(null, "Eggs are not supposed to be green."); }}
  11. 11. After Run the ProgramAfter Clicking the Test Button
  12. 12. public class ButtonListenerDemo2 { public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.setLayout(new FlowLayout()); final JTextField jtext = new JTextField(); jframe.getContentPane().add(jtext); jtext.setText("Hello"); jframe.setVisible(true); final int i=0; class MyButtonListener implements ActionListener{ int j = i; @Override public void actionPerformed(ActionEvent arg0) { jtext.setText(String.valueOf(j++)); } } JButton jbutton = new JButton("Test button"); jframe.add(jbutton); jbutton.addActionListener(new MyButtonListener()); }}
  13. 13. After Run the ProgramAfter Clicking the Test Button for Once
  14. 14. After Clicking the Test Button for TwiceAfter Clicking the Test Button for Thrice
  15. 15. Advantages of using Inner classImproves code-cohesionProvides convenience to the developer

×