Lecture 7Collections       Object Oriented Programming        Eastern University, Dhaka                Md. Raihan Kibria
An example import java.util.ArrayList; public class CollectionsDemo1 {     public static void main(String[] args) {       ...
What else can we do with ArrayListimport java.util.ArrayList;public class CollectionsDemo1 {    public static void main(St...
Can we keep any kind of objects in          a collection?   public class CollectionsDemo2 {       public static void main(...
The output
Cant we have plain array of                objects in java?  public class CollectionsDemoWithout {      public static void...
The output
Difference between array and an                Arraylist•    An array is of fixed size•    An ArrayList can grow and reduc...
QuestionWhich one is flexible: Array or ArrayList?Which one is faster: Array or ArrayList?
AnswerArrayList is more flexible since we candynamically change its dimension or sizeArray is faster because JVM needs to ...
What else is there under           collectionsHashSetTreeSetQueueHashMapetc.
An introduction to InterfacesAn example of interface:Interface GiveText{  String GiveText();}
public class CollectionsDemoInterface {  public static void printText(GiveSomething giveSomething){    System.out.println(...
OutputOrangeAppleGrapes
Why do we need InterfacesSometimes we do not know what our actualobject is but we still want some operationdone; for examp...
Why do we need InterfacesFor example, we have two types of list:ArrayList and LinkedList. These two classeswere developed ...
Example of List interfaceList myList = new ArrayList();List yourList = new LinkedList();Please note the left side of the a...
A list that contains different types                 of objectspublic class CollectionsDemoInterface {  public static void...
interface GiveSomething{String giveText();}class X implements GiveSomething{public String giveText() {return "Orange";}}cl...
for (GiveSomething g : myGenericList)   System.out.println(g.giveText()); Will print: Orange Apple Grape
Upcoming SlideShare
Loading in...5
×

Oop lecture7

664

Published on

Published in: Education
1 Comment
0 Likes
Statistics
Notes
  • Extremely useful. Java snippets are a great help. Thank you.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
664
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
43
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Oop lecture7"

  1. 1. Lecture 7Collections Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria
  2. 2. An example import java.util.ArrayList; public class CollectionsDemo1 { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); for (int i=0; i<10; i++) arrayList.add(new Integer(i)); for (int i=0; i<arrayList.size(); i++) System.out.println(arrayList.get(i)); } } }ArrayList is a collection. The aboveexample prints 0 to 9
  3. 3. What else can we do with ArrayListimport java.util.ArrayList;public class CollectionsDemo1 { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); for (int i=0; i<10; i++) arrayList.add(new Integer(i)); for (int i=0; i<arrayList.size(); i++) System.out.println(arrayList.get(i)); arrayList.remove(0); for (int i=0; i<arrayList.size(); i++) System.out.println(arrayList.get(i)); }}The last segment prints 1 to 9 because wehave removed the object at index 0
  4. 4. Can we keep any kind of objects in a collection? public class CollectionsDemo2 { public static void main(String[] args) { ArrayList list = new ArrayList(); for (int i=0; i<4; i++) list.add(new JButton(String.valueOf(i))); JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.getContentPane().setLayout(new FlowLayout()); for (int i=0; i<list.size(); i++){ jframe.getContentPane().add((JButton)list.get(i)); } jframe.setVisible(true); } }We have added JButton objects into the ArrayList list
  5. 5. The output
  6. 6. Cant we have plain array of objects in java? public class CollectionsDemoWithout { public static void main(String[] args) { JButton[] myButtons = new JButton[]{new Jbutton("0") ,new JButton("1"), new JButton("2"), new JButton("3"), }; JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setBounds(0, 0, 300, 200); jframe.getContentPane().setLayout(new FlowLayout()); for (int i=0; i<myButtons.length; i++){ jframe.getContentPane().add(myButtons[i]); } jframe.setVisible(true); } }Yes we can
  7. 7. The output
  8. 8. Difference between array and an Arraylist• An array is of fixed size• An ArrayList can grow and reduce in size• Any collection can grow and reduce in size but arrays cannot• i.e. ArrayList list = new ArrayList(); list.add(new Integer()); //is allowed• But this is not allowed: Integer[] intArr = new Integer[3]; intArr.add(new Integer());
  9. 9. QuestionWhich one is flexible: Array or ArrayList?Which one is faster: Array or ArrayList?
  10. 10. AnswerArrayList is more flexible since we candynamically change its dimension or sizeArray is faster because JVM needs to do lesswork to maintain the size (size is pre-known)
  11. 11. What else is there under collectionsHashSetTreeSetQueueHashMapetc.
  12. 12. An introduction to InterfacesAn example of interface:Interface GiveText{ String GiveText();}
  13. 13. public class CollectionsDemoInterface { public static void printText(GiveSomething giveSomething){ System.out.println(giveSomething.giveText()); } public static void main(String[] args) { CollectionsDemoInterface.printText(new X()); CollectionsDemoInterface.printText(new Y()); CollectionsDemoInterface.printText(new Z()); }}interface GiveSomething{ String giveText();}class X implements GiveSomething{ public String giveText() { return "Orange"; }}class Y implements GiveSomething{ public String giveText() { return "Apple"; }}class Z implements GiveSomething{ public String giveText() { return "Grapes"; }}
  14. 14. OutputOrangeAppleGrapes
  15. 15. Why do we need InterfacesSometimes we do not know what our actualobject is but we still want some operationdone; for example, some method of that objectcalledThe main reason we do not know about theactual object beforehand (while coding) is thatsomeone else is developing that object
  16. 16. Why do we need InterfacesFor example, we have two types of list:ArrayList and LinkedList. These two classeswere developed by two different teams;however, the integrator developer has defineda common interface ListInterface List contains methods likeadd(Object o), add(int index, Object o)Therefore, both ArrayList and LinkedList hasthose methods
  17. 17. Example of List interfaceList myList = new ArrayList();List yourList = new LinkedList();Please note the left side of the assignmentscontains List interface and the right side theactual object.Since add(Object o) is defined in List interfacewe can do both myList.add(new Integer(1)); yourList.add(new Integer(1)));
  18. 18. A list that contains different types of objectspublic class CollectionsDemoInterface { public static void printText(GiveSomething giveSomething){ System.out.println(giveSomething.giveText()); } public static void main(String[] args) { List<GiveSomething>myGenericList = new ArrayList<GiveSomething>(); myGenericList.add(new X()); myGenericList.add(new Y()); myGenericList.add(new Z()); }} See next page for the remaining code
  19. 19. interface GiveSomething{String giveText();}class X implements GiveSomething{public String giveText() {return "Orange";}}class Y implements GiveSomething{public String giveText() {return "Apple";}}class Z implements GiveSomething{public String giveText() {return "Grapes";}}
  20. 20. for (GiveSomething g : myGenericList) System.out.println(g.giveText()); Will print: Orange Apple Grape
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×