0% found this document useful (0 votes)
6 views12 pages

Java ArrayList: Basics and Methods

The document provides an overview of ArrayLists in Java, detailing their characteristics, initialization methods, and common operations such as adding, accessing, modifying, and removing elements. It also covers advanced topics like multi-dimensional ArrayLists, sorting, and algorithms for specific problems like finding the container with the most water and pair sums in a sorted and rotated ArrayList. Various code snippets illustrate the concepts discussed throughout the document.

Uploaded by

apna jaat gaming
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)
6 views12 pages

Java ArrayList: Basics and Methods

The document provides an overview of ArrayLists in Java, detailing their characteristics, initialization methods, and common operations such as adding, accessing, modifying, and removing elements. It also covers advanced topics like multi-dimensional ArrayLists, sorting, and algorithms for specific problems like finding the container with the most water and pair sums in a sorted and rotated ArrayList. Various code snippets illustrate the concepts discussed throughout the document.

Uploaded by

apna jaat gaming
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

A built linear
data
>
in structure in
java
-

Arrays Arraylist
Sava collection Frameworry
-
fixed Size
2-dynamic size

primitive data types 2-primitive data types


--

Can be stored Can't be store


directly
* To use an
ArrayList , we need to import it from the
java util' package
.

import Java .

util .

ArrayList ;
* An ArrayListCan be initialized in Several
Days
1 >
-

Empty Initialization
ArrayList string) list = new
ArrayList() ;
2- Initialization with a specific capacity
.

Amzaylist <Integer> list =


new
ArrayList < >
(10) :

-
3
Initialization With Another Collection ;

Antaylist (String) list = new


Arraylists (
Arrays -
as List ("A" , "B", "C")
);
basic
* Some method of Arraylist.
.
2
Adding Element = list -

add / Element
.
2
AccessingElement =
list -

get (i) ,
indes
3
Modify Elements = list .

Set (1 "2"
:

Index new Element


-
4
Removing Elements list . Remove
(E index
or list .
remove ("A"]
remove first occurance of A

5-checking Size : list . Size Is ;

For (string item list) E


Iterating
:
6- over list =

0 0 Plitems ;
3
.
.
Print reverse of an ArrayList

list = 1 ,
2 . 3 , 4 , 5

be 5
Output should : ,
4 ,
3 2
. , 1

② using for loop

for (inti =
list .
Sizes-1 ; i<= 0 ;
i --)
&
printin (list get (i) "")
System .
Out .
+ ;

&
Find maximum in an array list

list = 2 , 5 , 9 , 3, 6

initialize an Variable
called may which stores

Integer initially
. MIN-VALUE ;

After each iteration , ye

Compare max withCurrent


element if ,

than may then ye have ,


Element is
bigger
to store if in max-
Swap two numbers in an arraylist

list :
[2 , 5 , 9 , 3 ,
63 ,
indes = 1 ,
indexes = 3

2 -

first initialize temp a

Variable of integer type


2-store one of index value
to the temp Variable
&
3-the value which stored 1st Step

3 2nd
>
Step
3rd Step

in temp ye have to ,

assignanother anaa b
4-then transfer the value
at
temp value
to theSacond
index -

Sorting in arraylist

[Link] ()
--


↓ 1

class method of the class

not
this line dort the list
Collection-
in Ascending order
But if pant
we to cort the list in
Descending order
↓ then ,
statement showed be
is a interface
in [Link]( list, [Link]());
java
-comparatorAno -

logic

Multi-dimensional arrayList
of multidimensional
This allows
ArrayList'to inCreate
java is essential an
'Arraylist' of
'ArrayList's . us and
manage
a
matries like
· structure there each element of the main
'Arraylist' is
itself an

ArrayList'

-Initialization ArrayList Integers Matrix new


Aragi,
Adding
Row :

Arrayhist<Integer) rows = new


Arcaglist (Arrays . As List
(1 ,
3);
2.

Arrayhist <Integers rows = new


ArrayList (Arrays .
Aslist (4 , 5 ,
6);

ArrayList <Integer) rowz new


ArrayList (Arrays Aslist (7 5, 9));
= -

matrix add (rows) :;


,

matrice Add (row2]


.

matrixe add (row3)-


;

#
Accessing Element at rowo Colum ,
I

int element (0) (2) i


Matrice-get get
= .

*
Modifying Elements at rows ,
Columns to 10

matrixe .

get (1) .
Set (2 ,
20) ;

*
Iterating the Elements over .

For (Arrayhist <Integer> row :


Matria) [
For (Integer Element :
row) &
System -

out-printin /Element + ""7 ;

3
Adding Elements to specific
# a now

matrice get (0)


.

add (4) ;
*
Adding a New Row

matrin add .

(400 ArrayList (Arrays as List


(10 ,
",
12/);
Container with most water Flipkart
For given is lines on X-axis ,
us h lines to #
form a Container such that it holds maximum
Water.

height = [1 . 8 . 6, 2 , 5 , 4 , 8 ,
3, 7)

>
= 7

58
& + = =xz =
49)

First Approach -
(Bicute Force)
-
The brute Force approach invowe
cheering an
possible pairs of lines
to find the maximum area.

-
Initialize the Maximum Area

Starts With 'MayArea' . Set to 0 .

2 Iterate over an
pairs of lines
>
-

using two nested loops : the Outer loop goes from Start
the to the the
of Array second last Element and ,

inner loop
goes
current outer
from theElement
right
the
after the
Element loop
the
to end of .
array
.
3 Calculate the Area of each
pair .

You Carcuate the width


>
(i j) lines
pair of
-

each ,
,

as the difference in indices -2 .


the
>
-

height of the Container is the minimum height


of the two line-min
(height lis height [j)),

There calculate the Area "Width


height" *
>
-

as

Y-update the Maximum Area -

>
If calculated Area
-

May Area the update


is
larger than the Current
,

'MaxArea'
5
Return the mayArea.
Check minimum
height
>
-

Check total width


-

from i to
;
-Calculate Area
Area
update maximum
if Current Area is larger
than the

refer Area previous -


one

my

Second Approach 12 pointer -

Approach)
>
-

The two pointer Apprach is a more-efficient Day to solve this


problem- achieving time complexity of O(n) compared to the 0 (42)
a

time complexity of the berte Force approach ,


This Approach uses two
pointers Starting at the beginning and end
>
-

of the Array and moves towards the Center to find the maximum
Area The key idea is to use the fact that the area is limited
.

increase the
by the shorter
the
line and to
try and potential area
by
moving pointers
.

dep1 . :
Initializing two pointers ie
left -o and right :

array
·

Length-s
also maxArea =
.
0

Step.: 2
Chile left right , calculate the minimum height then
Calculate Area

Step-3 :
update the may red
if Current Are is
larger than
previous one
.

StepY :
If left stick is less than the
right stick

increment left by I

Else
decreament right by I
J Step - 2

↓CalculateAreaLet me
theCurrent Fina
I Step-y
(shift
Shift
to
right/
to left
According to
(Step 3)
-

the Condition

Time Complexity = 0 (1)

Pair sum - ii pivot Element


-

alway
:
[08 93 ,
,
target =
16

&
tae
find if any pair in a sorted and rotated
ArrayList
has a
a

#
Step---
find the pivot point
=
Step2-
-

for pointers
Initializeone
-
pointer left should be starts at the smarest

Element , another
pointer right should start at the

largest element.
#
Step-
-
3

more
pointers to find theSum .

·
calculate the Sum
of element
pointed to
by
the two pointers
If theSum is to the
·

equal given
value ,

return pair

Ifthe Sumis less than


target left pointer to
·
,
move t
no

left =
(left + 1) % a /1 Modular Faithmatic
IftheSumis greater thou the
target the decrea e
·
, a

right- (4-right 1) % + U // Modular Arithmatic

·
Continue the
process until
two pointers meet.

Element
find the pivot
for left
I
and last element

for right pointer

>
Modular Anithmatic

You might also like