Oop lecture7

Preview:

DESCRIPTION

 

Citation preview

Collections

Lecture 7

Object Oriented ProgrammingEastern University, Dhaka

Md. Raihan Kibria

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

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

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

The output

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

The output

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

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

Question

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)

What else is there under collections

HashSetTreeSetQueueHashMapetc.

An introduction to Interfaces

An example of interface:

Interface GiveText{ String GiveText();}

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

Output

OrangeAppleGrapes

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

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

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

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

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

Will print:

OrangeAppleGrape

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