0% found this document useful (0 votes)
8 views17 pages

Java ArrayList and Wrapper Classes Guide

btech 2nd year java

Uploaded by

Vedanshi Aryan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

Java ArrayList and Wrapper Classes Guide

btech 2nd year java

Uploaded by

Vedanshi Aryan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topic of Discussion

CS202: IT Workshop
Java
Arrays and ArrayList

Ref:
1. Harvey Deitel, Paul Deitel, Java: How to Program, 9/e,
Prentice Hall India.
2. [Link]

1
Topic
Limitations
of Discussion
of array

Array has many usages; any limitation of array?

MG@IIITG 2
Limitations
Topic of
ofDiscussion
array: ArrayList
One of the limitation of array is its FIXED size
ArrayList is a data structure where elements can be
added, removed dynamically
In addition, it supports various in-built methods
We can create an ArrayList of only reference types
(as Objects)

*Java also supports other data structures like LinkedList, Stack, Vector, Set, etc.
MG@IIITG 3
Topic
ArrayList
of Discussion
in Java
One of the limitation of array is its FIXED size
ArrayList is a data structure where elements can be
added, removed dynamically
In addition, it supports various in-built methods
We can create an ArrayList of only reference types
What about primitive types (int, double etc.)?

MG@IIITG 4
Topic
ArrayList
of Discussion
in Java
$ yellow red green
ArrayList<String> items = new ArrayList<String>();
$ yellow green
[Link]("red");
[Link](0, "yellow"); $1
[Link]("green");

for ( int i = 0; i < [Link](); i++ ) add(item) inserts item at rear end
[Link]( [Link]( i ) );
add (pos, item) inserts at the pos
[Link](1);
remove(index) removes item
for ( int i = 0; i < [Link](); i++ ) given by index
[Link]( [Link]( i ) );
get(index) returns item of index
[Link] ( [Link](“green”) );
indexOf(item) returns the index of
[Link](); the first occurrence of item

clear() removes all items


MG@IIITG *we need to import “[Link]” 5
Topic
ArrayList
of Discussion
in Java

MG@IIITG 6
Code Demonstration ([Link])
Topic
ArrayList
of Discussion
in Java
 We can also create ArrayList of the Class we create in our
code (e.g. Student)
Code Demonstration ([Link])

MG@IIITG 8
Topic
Wrapper
of Discussion
class
 Many supported data structures in Java (such as
Arraylist) works only with Reference type
 Wrapper class converts (wraps) primitive type to its
equivalent Reference type
(int  Integer, double  Double, etc.)
 Class “Integer” contains the equivalent int as a field
within it (along with various other fields).
public final class Integer extends Number implements Comparable<Integer> {
.....
private final int value;
.....
public Integer (int value) {
[Link] = value; Definition of Class Integer
}
.....
(present in Java)
MG@IIITG 10
Wrapper
Topic ofclass
Discussion
example
 To create an Integer object from a primitive int
Integer myIntObj = new Integer(5); //old version

Integer myIntObjNew = 7; //new version


This is called
Autoboxing
 To get the equivalent int value from an Integer obj

int myPrimInt = [Link]();

int myPrimInt = myIntObj; //new version

This is called
Unboxing
MG@IIITG 11
Topic
Wrapper
of Discussion
class
 We can also use wrapper class for other primitive
data types

Character myCharObj = 'x';


char myChar = myCharObj;

[Link]( myCharObj );

MG@IIITG 12
Code Demonstration ([Link])
ArrayList
Topic and
of Discussion
Wrapper class
 Now, if we want to create an ArrayList of
“integers” (i.e. int)?

MG@IIITG 14
ArrayList
Topic and
of Discussion
Wrapper class
 Now, if we want to create an ArrayList of
“integers” (i.e. int)?
 We can create that using the wrapper class Integer

ArrayList<Integer> integerList = new ArrayList<Integer>();

[Link](24);

Code Demonstration ([Link])

MG@IIITG 15

You might also like