0% found this document useful (0 votes)
5 views2 pages

Java ArrayList Operations Guide

The document provides a comprehensive guide on using ArrayLists in Java, detailing various operations such as declaring, adding, getting, setting, deleting elements, and sorting the list. It includes code examples demonstrating these operations with Integer, String, and Boolean types. Additionally, it encourages readers to practice by solving homework problems related to arrays with ArrayLists.

Uploaded by

Fatima Ahmed
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)
5 views2 pages

Java ArrayList Operations Guide

The document provides a comprehensive guide on using ArrayLists in Java, detailing various operations such as declaring, adding, getting, setting, deleting elements, and sorting the list. It includes code examples demonstrating these operations with Integer, String, and Boolean types. Additionally, it encourages readers to practice by solving homework problems related to arrays with ArrayLists.

Uploaded by

Fatima Ahmed
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

‭ArrayList in Java‬

‭Operations :‬

1‭ .‬ ‭ eclare an ArrayList of different Types‬


D
‭2.‬ ‭Add Element‬
‭3.‬ ‭Get Element‬
‭4.‬ ‭Add Element at a specific Index‬
‭5.‬ ‭Set Element at a specific Index‬
‭6.‬ ‭Delete Element from an Index‬
‭7.‬ ‭Size of the List‬
‭8.‬ ‭Loop/Iterate on the List‬
‭9.‬ ‭Sort the List‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ A
‬rrayList‬
;‬

import‬‭
‭ java‬
.‭
‭u
‬til‬
.‭
‭ C
‬ollections‬
;‬

class‬‭
‭ ArrayLists‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‭
‭ S
‬tring‬‭
args‬
[]) {‬

ArrayList‬
‭ <‭
‭I‬nteger‬
>‬‭
‭ list‬‭
=‬‭
new‬‭
ArrayList‬
<‭
‭ I
‬nteger‬
>();‬

ArrayList‬
‭ <‭
‭S‬tring‬
>‬‭
‭ list2‬‭
=‬‭
new‬‭
ArrayList‬
<‭
‭ S
‬tring‬
>();‬

ArrayList‬
‭ <‭
‭B‬oolean‬
>‬‭
‭ list3‬‭
=‬‭
new‬‭
ArrayList‬
<‭
‭ B
‬oolean‬
>();‬

//add elements‬

list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 1
‬‭
)‬;‬
list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 3
‬‭
)‬;‬
list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 4
‬‭
)‬;‬
list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 5
‬‭
)‬;‬
System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ l
‬ist‬
);‬

//to get an element‬



int‬‭
‭ element‬‭
=‬‭
list‬
.‭
‭ g
‬et‬
(‬
‭ 0‭
‭)‬;‬‭
// 0 is the index‬
System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ e
‬lement‬
);‬

//add element in between‬



list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 1
‬‭
,‬‭
2
‬‬);‬‭
‭ // 1 is the index and 2 is the‬‭
element to be added‬
System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ l
‬ist‬
);‬

//set element‬

‭Apna College‬
list‬
‭ .‭
‭s
‬et‬
(‭
‭ 0
‬‭
,‬‭
0
‬‬);‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ l
‬ist‬
);‬

//delete elements‬

list‬
‭ .‭
‭r
‬emove‬
(‬
‭ 0‭
‭)‬;‬‭
// 0 is the index‬
System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ l
‬ist‬
);‬

//size of list‬

int‬‭
‭ size‬‭
=‬‭
list‬
.‭
‭ s
‬ize‬
();‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ s
‬ize‬
);‬

//Loops on lists‬

for‬
‭ (‭
‭i‬nt‬‭
i‭
=
‬‭
0‬‭
;
‬‬‭
i‭
<
‬‭
l‬ist‬
.‭
‭ s
‬ize‬
();‬‭
‭ i‭
+‬+) {‬
System‬
‭ .‭
‭o‬ut‬
.‭
‭ p
‬rint‬
(‭
‭l‬ist‬
.‭
‭ g
‬et‬
(‭
‭ i
‬‭
)‬ +‬‭
" "‬
);‬

}‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
();‬

//Sorting the list‬



list‬
‭ .‭
‭a
‬dd‬
(‭
‭ 0
‬‭
)‬;‬
Collections‬
‭ .‬
‭ sort‬
‭ (‬
‭ list‬
‭ );‬

System‬
‭ .‬
‭ out‬
‭ .‭
‭p‬rintln‬
(‭
‭ l
‬ist‬
);‬

}‬

}‬

‭Homework Problems‬
‭Try solving all problems of arrays with arraylists.‬

‭Apna College‬

You might also like