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 above
example prints 0 to 9
3. What else can we do with ArrayList
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.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
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
6. 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
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. Question
Which one is flexible: Array or ArrayList?
Which one is faster: Array or ArrayList?
10. Answer
ArrayList is more flexible since we can
dynamically change its dimension or size
Array is faster because JVM needs to do less
work to maintain the size (size is pre-known)
11. What else is there under
collections
HashSet
TreeSet
Queue
HashMap
etc.
12. An introduction to Interfaces
An example of interface:
Interface GiveText{
String GiveText();
}
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";
}
}
15. 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
called
The main reason we do not know about the
actual object beforehand (while coding) is that
someone else is developing that object
16. 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
17. 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)));
18. 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
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. for (GiveSomething g : myGenericList)
System.out.println(g.giveText());
Will print:
Orange
Apple
Grape