0% found this document useful (0 votes)
15 views

Arrays in Java

The document provides an overview of arrays in Java, explaining their characteristics such as fixed size, same type elements, and zero-based indexing. It covers various methods for declaring and initializing arrays, accessing elements, and manipulating them, including sorting and searching techniques. Additionally, it discusses single-dimensional and multi-dimensional arrays, including jagged arrays.

Uploaded by

hritikasharma856
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)
15 views

Arrays in Java

The document provides an overview of arrays in Java, explaining their characteristics such as fixed size, same type elements, and zero-based indexing. It covers various methods for declaring and initializing arrays, accessing elements, and manipulating them, including sorting and searching techniques. Additionally, it discusses single-dimensional and multi-dimensional arrays, including jagged arrays.

Uploaded by

hritikasharma856
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
You are on page 1/ 11

cIASSMAte

Date :
Page:

ARRAYS IN JAVA
Am array in Java is a collection o variables
f the Same type stored in contiquous
memory location Arrays allow you to store
muliple values in
in a single Variable rather
Hhan' declaing separaBe variables for each
value.

Keys Points About Arrays


Eixed Size Omce n array is createdl , its size
SIze

cannot be charged
Same Type : AII elements in an array must be
CHthe same ype (eg all integers.,
all strings)
Arraysfistare elemernt
Indexing the zero- indexed
has_an
meaning
index o0.

)Declar ing and initializing Arrays


You can declare and initialize an array in multipl
ways.

@ uriaus-programmeT
integers
ofarray
all index.
an
using
cIASSMAte values
predefined
integers, with
4,5}j/Array
E1,2,3, :1040output:
loutput
Page:
Date:
o
5
to 0. progra
at 4050
I
an of initialized [31);
[o1);
o Arros accessed
starts
declaration elements
Array
Accessing
an
array l (arr(arr
30,
-:
[5];
int index 20, println Curiou
[5];
are out-println
Jl 10,
i
int
b)Imitializing 2:
values:
With the R
elements @
system-oul-
ar size:new
1.
With =
arr
arr system.
Remember
[] = L1 L]
int arr int
Array int
programmer 5040
@
urious-
arr.length) lop
clASSMAte property. 30
ot for 20
10
:DatePage:
number :"+ a 50 :loutput
i+)¬ 40 i
length using j
length 50
30
,50
(i.e.,
the 40,
(arrLil);
println
Systm.out- (num);
enhanced
for-each
loop i 10,20
arr.length 40
he 5};
=1,2,3,4
inttlarr
("Array arrays
,
20,30 1oap: 30,
using
array Arrays
through
Loping output:
20,
outprint
printn through loopi i=0ii< for-eoch num:arr)
for
Cint
=10, 10,
anacessed for Il
Array
lengthof oul- loop = systcm.
length System. Regulararr(int arr
Enhanced
be can
You
can I] [J
jntfor int
Thecan a) b)
Or
Dimensionalelement
aray
clASSMAte is
each
tovalues directly:
array) index. an
:DatePage:
Single- Dedaring @
Curious
-programMeY
ArraysE
(1D ts values
by 50
40,
a I/
Assigning
Il
array element with
Initializing
[s];
elements
Dimensiomal 30
int i N10,20,
-dimensional
each iniialize
new
o accessand i
= 10 20 30j
;40 50;
=
- :ist int
]arr
= [1] = :[3
= [2]
= arr
Can arr
Single
single Declacing
Simple"
can
L01 4]
arr
you 1ntii
arrarr
arr
1 You 0r,
A
Accessing Mutti-Dimensional
Declaing mostThe Arrays:
A 2.
system.
Systern-out- int 2Dmulti
[I] array
outprintn dimensional -
Elements commm
mitializing
matrix and
rogrammer
urious @ printin {1,2.3},
456}
7,8, 93 (think

output
(matrix(matrixW
in ofarray
muli
output of
:9 a a -
:1
[21[21)Iol[O]): 2D dimensianal
table 1s
(thid Arrays2D
a an
(irst
; Array: arrays
oraray of
row, column)
row,irst
matrix). Page: Date:
cIASSMAte
hird aray

olumn) 1S
Row L1,2
Row 1:Rouw0:Index Array Multi
Represerntation
Memory : Representatian
ValueIndexMenory
: : Array Single
2: :
: - :
I7,8, :
Dimensional : -
0 int Q int
@4,5,6 12,3}, 0123
[20 4 Dimensiomal
[1
s 1 7,8 4,5 6}I]L1
(30 arr
91 2 matrix 40]
3]
9} Array: 50] 10=
- Array:
r 20
{=
30,
40,50}
Page: Date :
cIASSMAte
row
Unlike is A
int numbers
jagged
aray
jaggedArray
Jagged [ILI JAGGED
Jagged
Araus 3 (inttLooping
tohr cough
has an for
ged
aray Syskem.
out
System-out Cint
ArTayArray jagged he
-dimensional
multi aray i=0;i<matrix
-lengthi
it+){
j
- I2]= 0]
L1] [Aray samne =0
allows
columns. uhose
[3]i = = (also 1<matrix
printn a
new neuw new = printn 0i l
outpuE: 2D
new ows elements
columns
number
int int int known /moves Arcay
L21 int (matrix Ci].
[1]; 12 :
towhere
aray rOw
to length
[311I are as the 3
an 45
LiJLj]
have next i
6 j++)
themselves.
arrays
arrays")
"array of line 897 + Page Date
cIASSMAte
different ");
E
afer
eYery
each
columa)
cIASSMAte "); 6
jt+)
row,hid 45
LilLj1+
:1aggod
Anray ilength 123
itt)
:Date Page:
i)
Anrayii (jaggedArray @
Curiou
-prog
:dizectly [:l[2] i<jaggedArray.length
l1
(secomd :
Array jaggedArray
loutput
Jagged
= :5
(jagged (O;
atprinthn
System.
Array {3,4,
înitialize
a println
5}, in output
a <
1a0ged
S1,23
Elenaiks printhn
l ihraugh / SysBern.out.
j-0
also IL] i=0i
can AcessingSystem.oul Looping Cind
(int
int for
You for
elements mehod
desceñdmg):
cIASSMAte ;
arrange () (arr)li )
[i1
)
sort { Cmatik
:DatePage:
4,5,8] itt)
to or Arrays. String rous): ow
Techniques 5,2,8_,1,4} (); String
operatian
Common
(ascencing to i<matrix.leng
soTt
Ileach
[12, Cby
CArays.
the {9,8,Lil);to
7};
(Arrays.
Cmatrix
Manipulation a1D
Array Arroy 3{65
4},
23,
1,
use :
loutput
aSoring
Arrays
) order -printha
(arr)
sort
Arrays printhn
Can 2D =
SoTt
arrays. matrix
[IL]
int sort
specificyou 0;
= system.out out-
-Arrays
arr (inte
Array Soring
is java, Sorking System-
[]
Sorting
for
int
4- Jmto
in
i);
an
array
+ 2. ");}
cIASSMAte search index"
index
@
Curious
-programe
foumd
:DatePage: : at at
in binary arrays)
element found
tound
i++) not
50j
unsorted i
("Element
Element ('Element
ar , isarr.length
40 i
Arrayssearch
tor
an search false
=10,20,30, )
target
:
Outout
System.out.printn
(for for
sorbed
arrays).
printm
( 30 = I
in found * tue
foumd
Iinear Search = i (arri] System.o
target =0
=
Searching i =
break
i if(found)
6][7,8,9]
[4,5, booleamCint
L1,2,3J int[larr
Output can Linear if
using int tor
b) You
cIASSMAte
Date :
Page:

Binary Search (ior sorted arrays):


int []] arr = 1, 2, 4, 5, 8}i
int target = 4i
int index = Arrays. binary Search (arr ,target) ;
SysBen. out printlm ("Element found at index
+ index);
I output:2 (index of 4)

@Curious- programmer

You might also like