20
Collections Lecture 7 Object Oriented Programming Eastern University, Dhaka Md. Raihan Kibria

Oop lecture7

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Oop lecture7

Collections

Lecture 7

Object Oriented ProgrammingEastern University, Dhaka

Md. Raihan Kibria

Page 2: Oop lecture7

An exampleimport 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 above example prints 0 to 9

Page 3: Oop lecture7

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 we have removed the object at index 0

Page 4: Oop lecture7

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

Page 5: Oop lecture7

The output

Page 6: Oop lecture7

Can't 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

Page 7: Oop lecture7

The output

Page 8: Oop lecture7

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());

Page 9: Oop lecture7

Which one is flexible: Array or ArrayList?Which one is faster: Array or ArrayList?

Question

Page 10: Oop lecture7

Answer

ArrayList is more flexible since we can dynamically change its dimension or sizeArray is faster because JVM needs to do less work to maintain the size (size is pre-known)

Page 11: Oop lecture7

What else is there under collections

HashSetTreeSetQueueHashMapetc.

Page 12: Oop lecture7

An introduction to Interfaces

An example of interface:

Interface GiveText{ String GiveText();}

Page 13: Oop lecture7

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"; }}

Page 14: Oop lecture7

Output

OrangeAppleGrapes

Page 15: Oop lecture7

Why do we need Interfaces

Sometimes we do not know what our actual object is but we still want some operation done; for example, some method of that object calledThe main reason we do not know about the actual object beforehand (while coding) is that someone else is developing that object

Page 16: Oop lecture7

Why do we need Interfaces

For example, we have two types of list: ArrayList and LinkedList. These two classes were developed by two different teams; however, the integrator developer has defined a common interface List

Interface List contains methods like add(Object o), add(int index, Object o)

Therefore, both ArrayList and LinkedList has those methods

Page 17: Oop lecture7

Example of List interface

List myList = new ArrayList();List yourList = new LinkedList();

Please note the left side of the assignments contains List interface and the right side the actual object.

Since add(Object o) is defined in List interface we can do both myList.add(new Integer(1)); yourList.add(new Integer(1)));

Page 18: Oop lecture7

A list that contains different types of objects

public 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

Page 19: Oop lecture7

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";}}

Page 20: Oop lecture7

Will print:

OrangeAppleGrape

for (GiveSomething g : myGenericList) System.out.println(g.giveText());