You are on page 1of 37

What’s‌a‌ n‌A

‌ rray‌L‌ ist‌‌   ‌
‌ rrayList‌i‌s‌   ‌ ‌
•‌A
–‌a‌ ‌‌class‌‌in‌‌the‌‌standard‌‌Java‌‌libraries‌‌that‌‌can‌‌hold‌  ‌
any‌‌type‌‌of‌‌object‌‌   ‌
–‌a‌ n‌‌object‌‌that‌‌can‌‌grow‌‌and‌‌shrink‌‌while‌‌your‌  ‌
program‌‌is‌‌running‌‌(unlike‌‌arrays,‌‌which‌‌have‌‌a ‌ ‌
fixed‌‌length‌‌once‌‌they‌‌have‌‌been‌‌created)‌‌   ‌
•‌I‌n‌‌general,‌‌an‌A
‌ rrayList‌s‌erves‌‌the‌‌same‌  ‌
purpose‌‌as‌‌an‌‌array,‌‌except‌‌that‌‌an‌   ‌ ‌
ArrayList‌c‌an‌c‌ hange‌l‌ength‌w ‌ hile‌‌ 
the‌ p
‌ rogram‌‌is‌‌running‌‌   ‌
42‌  ‌

Using‌‌the‌A
‌ rrayList‌C
‌lass‌‌   ‌
•‌I‌n‌‌order‌‌to‌‌make‌‌use‌‌of‌‌the‌A
‌ rrayList‌c‌lass,‌‌it‌‌must‌  ‌
first‌b
‌ e‌i‌mported‌   ‌ ‌
import‌‌ java.util.ArrayList;‌‌‌   
•‌A‌ n‌A‌ rrayList‌i‌s‌‌created‌‌and‌‌named‌‌in‌‌the‌‌same‌‌way‌  ‌
as‌‌object‌‌of‌‌any‌‌class,‌‌except‌‌that‌‌you‌‌specify‌‌the‌‌base‌  ‌
type‌‌as‌‌follows:‌‌   ‌
ArrayList<BaseType>‌‌
aList‌‌

‌  
‌‌
new‌‌
ArrayList<BaseType>();‌‌‌
  
43‌  ‌

Creating‌‌an‌A
‌ rrayList‌‌‌
  
•‌A
‌ n‌i‌nitial‌c‌ apacity‌c‌ an‌b
‌ e‌s‌ pecified‌w‌ hen‌c‌ reating‌a‌ n‌A ‌ rrayList‌a‌s‌  ‌
well‌‌   ‌
–‌T‌ he‌‌following‌‌code‌‌creates‌‌an‌A ‌ rrayList‌t‌hat‌‌stores‌‌objects‌‌of‌‌the‌  ‌
base‌‌type‌S ‌ tring‌w ‌ ith‌‌an‌‌initial‌‌capacity‌‌of‌‌20‌‌items‌‌  
ArrayList<String>‌‌ list‌‌ =‌‌ new‌‌ ArrayList<String>(20);‌– ‌  ‌‌
Specifying‌‌an‌‌initial‌‌capacity‌‌does‌‌not‌‌limit‌‌the‌‌size‌‌to‌‌which‌‌an‌  ‌
ArrayList‌c‌an‌‌eventually‌‌grow‌‌   ‌
44‌  ‌

The‌A
‌ rrayList‌‌
Class‌‌   ‌
•‌A
‌ rray‌‌data‌‌structure:‌‌   ‌
–‌P
‌ ro:‌‌Can‌‌access‌‌any‌‌element‌‌with‌‌its‌‌index‌‌   ‌
–‌C
‌ on:‌‌Cannot‌‌add‌‌or‌‌remove‌‌elements‌‌(statically‌‌sized)‌‌ 
•‌L‌ inked‌‌list‌‌data‌‌structure‌‌   ‌
–‌A
‌ ‌l‌inked‌‌list‌‌‌is‌‌a‌‌data‌‌structure‌‌in‌‌which‌‌an‌‌element‌‌of‌‌the‌  ‌
list‌‌is‌‌a‌n
‌ ode‌‌‌that‌‌references‌‌the‌‌next‌‌element‌‌in‌‌the‌‌list.‌•‌ ‌A‌  ‌‌
node‌‌‌in‌‌a‌‌linked‌‌list‌‌is‌‌made‌‌up‌‌of‌‌some‌‌data‌‌and‌‌a‌‌reference‌‌to‌  ‌
another‌‌node.‌‌   ‌
–‌P
‌ ro:‌‌Can‌‌add‌‌and‌‌remove‌‌elements‌‌(dynamically‌‌sized)‌– ‌  ‌‌
Con:‌‌Have‌‌to‌‌traverse‌‌list‌‌from‌‌beginning‌‌to‌‌find‌‌an‌‌element‌•‌  ‌‌
The‌‌Java‌‌provides‌‌a‌‌class‌‌called‌A ‌ rrayList‌ .‌–‌ ‌A
‌ n‌‌ 
ArrayList‌‌‌is‌‌a‌‌data‌‌structure‌‌that‌‌is‌‌a‌‌combination‌‌of‌‌an‌ a‌ rray‌‌and‌‌ 
a‌‌linked‌‌list.‌  ‌

45‌‌   ‌

The‌A
‌ rrayList‌‌
Class‌‌   ‌
‌ rrayLists‌c‌ontain‌‌elements‌‌that‌‌can‌‌be‌‌accessed‌‌by‌‌their‌  ‌
•‌A
index,‌‌but‌‌also‌‌have‌‌advantages‌‌over‌‌arrays:‌‌   ‌
–‌A
‌ n‌A
‌ rrayList‌a‌utomatically‌‌expands‌‌as‌‌items‌‌are‌‌added‌‌to‌‌it.‌‌ 
–‌A‌ n‌A ‌ rrayList‌a‌utomatically‌‌shrinks‌‌as‌‌items‌‌are‌‌removed‌‌from‌  ‌
it.‌‌   ‌
•‌T‌ o‌‌use‌‌an‌A
‌ rrayList‌,‌‌you‌‌must‌‌import‌‌it‌‌from‌‌the‌  ‌
java.util‌p ‌ackage:‌‌   ‌
import‌j
‌ava.util.ArrayList;‌‌‌
  
•‌T‌ he‌‌declaration‌‌and‌‌creation‌‌of‌‌an‌A
‌ rrayList‌f‌ollows‌‌this‌  ‌
form:‌‌   ‌
ArrayList<‌
dataType‌
>‌i
‌dentifier‌=
‌‌n
‌ew‌A
‌rrayList<‌
dataType‌
>();‌‌‌
  
•‌S‌ o‌‌If‌‌I‌‌wanted‌‌to‌‌create‌‌an‌‌array‌‌list‌‌whose‌‌elements‌‌are‌‌of‌‌type‌  ‌
String‌ :  ‌‌ ‌
ArrayList<String>‌‌
myList‌‌
=‌n
‌ew‌A
‌rrayList<String>();‌‌‌
  
•‌I‌mportant‌‌Note:‌‌An‌A‌ rrayList‌c‌an‌‌only‌‌hold‌‌elements‌‌that‌  ‌
are‌o
‌ bjects‌.  ‌‌ ‌
–‌T‌ hat‌‌means‌‌an‌A
‌ rrayList‌c‌annot‌‌hold‌‌elements‌‌of‌‌a‌‌primitive‌  ‌
type.‌ 
46‌‌   ‌

The‌A
‌ rrayList‌‌
Class‌‌   ‌
•‌Y‌ ou‌‌can‌‌add‌‌items‌‌to‌‌the‌‌end‌‌of‌‌an‌A
‌ rrayList‌b
‌y‌  ‌
using‌‌the‌a
‌ dd‌m ‌ ethod‌‌   ‌
myList.add(‌ "Eric"‌ );‌‌‌
  
myList.add(‌ "Bob"‌ );‌
  ‌
myList.add(‌ "Steve"‌ );‌‌‌
  
–‌T‌ his‌‌will‌‌make‌"
‌ Eric"‌t‌he‌‌first‌‌element,‌" ‌ Bob"‌t‌he‌  ‌
second‌‌element,‌‌and‌" ‌ Steve"‌t‌he‌‌third‌‌element.‌– ‌ ‌L‌ ike‌‌in‌‌an‌‌ 
array,‌‌these‌‌elements‌‌have‌‌indices‌‌starting‌‌at‌‌0.‌•‌ ‌T
‌ o‌‌get‌‌the‌‌ 
number‌‌of‌‌items‌‌stored‌‌in‌‌the‌A ‌ rrayList‌ ,‌ ‌use‌‌the‌‌ 
size‌m ‌ ethod.‌‌   ‌
myList.size();‌‌‌   
•‌T‌ o‌‌get‌‌an‌‌element‌‌by‌‌its‌‌index,‌‌use‌‌the‌g
‌ et‌m ‌ ethod‌‌ 
myList.get(1);‌‌‌   
•‌E‌ xample:‌‌ArrayListExample.java‌  ‌
47‌‌   ‌

Enhanced‌‌for‌‌Loop‌‌and‌   ‌ ‌
ArrayLists‌‌‌   
•‌A
‌ s‌‌stated‌‌previously,‌‌the‌‌enhanced‌f ‌ or‌‌  
loop‌i‌terates‌t‌ hrough‌t‌ he‌e‌ lements‌o
‌ f‌a‌ ny‌  ‌
collection.‌‌   ‌
–‌W
‌ e’ve‌‌seen‌‌this‌‌with‌‌arrays‌‌before:‌‌   ‌
String[]‌‌
names‌‌
=‌‌
{"
‌Eric"‌
,‌"
‌Thomas"‌
,‌"
‌Steve"‌
};‌‌‌
  

for‌
(String‌‌
name‌‌
:‌‌
names)‌‌
{  
‌‌‌
System.‌
out‌
.println(name);‌‌‌
  
}‌‌‌
  
–‌W
‌ e‌‌can‌‌also‌‌do‌‌this‌‌with‌‌ 
ArrayLists‌ .‌•‌ ‌‌Example:‌‌ 
EnhancedForArrayList.java‌  ‌
48‌‌   ‌

Inserting,‌‌Removing‌‌and‌‌Replacing‌‌Elements‌‌   ‌
•‌T‌ o‌‌remove‌‌an‌‌element‌‌from‌‌a‌‌list‌‌you‌‌use‌‌the‌r
‌ emove‌m
‌ ethod‌  ‌
with‌‌the‌‌index‌‌of‌‌the‌‌element‌‌you‌‌want‌‌to‌‌remove:‌‌ 
myList.remove(1);‌‌‌   
–‌N
‌ ote:‌‌This‌‌will‌‌remove‌‌the‌‌second‌‌element‌‌and‌‌all‌‌elements‌  ‌
behind‌‌that‌‌element‌‌will‌‌move‌‌up‌‌in‌‌the‌‌list.‌‌   ‌
•‌F‌ or‌‌example:‌‌If‌‌an‌‌element‌‌has‌‌index‌‌5‌‌before‌‌we‌‌remove‌‌the‌‌element‌‌at‌  ‌
index‌‌1,‌‌after‌‌the‌‌removal,‌‌it‌‌will‌‌have‌‌index‌‌4.‌‌   ‌
•‌T‌ o‌‌insert‌‌an‌‌element‌‌into‌‌a‌‌specific‌‌index‌‌you‌‌use‌‌the‌a
‌ dd‌‌
 
method‌‌(similar‌‌to‌‌adding‌‌an‌‌element‌‌to‌‌the‌‌end‌‌of‌‌the‌‌list):‌‌ 
myList.add(3,‌" ‌Chris"‌ );‌‌‌  
–‌T‌ his‌‌will‌‌add‌"
‌ Chris"‌t‌o ‌ ‌‌the‌‌list‌‌at‌‌index‌‌3‌‌and‌‌all‌‌elements‌‌with‌  ‌
indices‌‌greater‌‌than‌‌2‌‌will‌‌move‌‌back‌‌one.‌‌  
•‌F‌ or‌‌example:‌‌If‌‌an‌‌element‌‌has‌‌index‌‌5‌‌before‌‌we‌‌add‌‌the‌‌element‌‌at‌  ‌
index‌‌3,‌‌after‌‌the‌‌addition,‌‌it‌‌will‌‌have‌‌index‌‌6.‌‌   ‌
•‌F‌ inally,‌‌you‌‌can‌‌replace‌‌an‌‌element‌‌by‌‌using‌‌the‌s
‌ et‌m
‌ ethod:‌‌ 
myList.set(2,‌" ‌Joe"‌ );‌‌‌
  
–‌T‌ his‌‌will‌‌replace‌‌the‌‌value‌‌of‌‌the‌‌element‌‌at‌‌index‌‌2‌‌with‌‌the‌‌value‌  ‌
"Joe"‌ .  ‌‌ ‌
•‌E‌ xample:‌‌ArrayListInsertRemoveReplace.java‌  ‌
49‌‌   ‌

Array‌L‌ ists‌‌   ‌
Automatically‌‌keeps‌‌track‌‌of‌‌the‌‌list‌c‌ apacity‌(‌ the‌  ‌
length‌‌of‌‌the‌‌allocated‌‌array)‌‌and‌‌list‌s‌ ize‌(‌ the‌  ‌
number‌‌of‌‌elements‌‌currently‌‌in‌‌the‌‌list)‌  ‌

capacity‌‌   ‌
size‌‌   ‌
“Sue"‌‌
“Bob"‌‌
“Joe"‌‌‌
  

50‌‌   ‌

Array‌L‌ ists‌‌   ‌
When‌‌data‌‌is‌‌added‌‌at‌‌the‌‌end‌‌of‌‌the‌‌array‌‌list,‌  ‌
the‌‌capacity‌‌is‌‌checked.‌‌If‌‌it‌‌is‌‌full‌‌it‌‌adds‌‌space‌  ‌
(doubles)‌‌and‌‌then‌‌adds‌‌the‌‌new‌‌data‌‌reference‌  ‌
to‌‌the‌‌end.‌‌   ‌

capacity‌‌   ‌
“Joe"‌‌‌
  
“Jim"‌‌
 
“Sue"‌‌
“Bob"‌‌
 

51‌‌   ‌

Array‌L‌ ists‌‌   ‌
Method‌‌
Summary‌‌
for‌‌
deleting‌‌
data‌‌‌
  
remove‌(int‌‌
index)‌R
‌emoves‌‌
the‌‌element‌‌
at‌‌
 
the‌ ‌
specified‌‌position‌‌
from‌‌
this‌‌list.‌‌‌
  

Removes‌‌the‌‌designated‌‌element‌‌and‌‌moves‌‌all‌‌others‌‌down‌‌in‌‌index‌‌value‌‌to‌‌fill‌  ‌
in‌‌the‌‌gap.‌‌   ‌
newFriends.remove(0);‌‌‌   
capacity‌  ‌

“B‌ob‌"
“Jim"‌‌ ‌ ‌‌
“Joe"‌‌‌
  

“Sue”‌‌removed‌‌   ‌
52‌‌   ‌
ArrayList‌M
‌ ethods‌‌   ‌

Return‌‌value‌‌Method‌‌name‌‌and‌‌argument‌‌list‌‌
 
boolean‌‌
add(‌E
‌‌e
‌lement‌‌
)  
‌‌‌
a
‌ ppends‌e
‌ lement‌t‌o‌‌the‌‌end‌‌of‌   ‌ ‌
the‌‌list‌‌
   ‌
void‌‌
clear(‌‌
)  
‌‌‌
r‌emoves‌‌all‌‌the‌‌elements‌‌in‌‌the‌   ‌ ‌
list‌‌
   ‌
int‌‌
size(‌‌
)  
‌‌‌
r‌eturns‌‌the‌‌number‌‌of‌‌elements‌   ‌ ‌
E‌r
‌emove(‌‌
int‌‌ )‌ r‌emoves‌‌the‌‌element‌‌at‌‌
index‌‌  
the‌   ‌ ‌
specified‌i‌ndex‌p
‌ osition‌  ‌
53‌‌   ‌

More‌A
‌ rrayList‌M
‌ ethods‌‌  
Method‌‌name‌‌and‌‌ 
argument‌‌list‌‌
   ‌
Return‌ v
‌ alue‌‌
   ‌

E‌g
‌et(‌‌
int‌‌
index‌‌
)  
‌‌‌
r‌eturns‌‌the‌‌element‌‌at‌‌the‌‌specified‌   ‌ ‌
index‌p ‌ osition;‌‌the‌‌element‌‌is‌‌not‌‌removed‌  ‌
from‌‌the‌‌list.‌‌
   ‌
E‌s
‌et(‌‌
int‌‌
index,‌E
‌‌e )‌ r‌eplaces‌‌the‌‌
‌lement‌‌  
element‌a
‌ t‌‌the‌‌specified‌   ‌ ‌
index‌p
‌ osition‌‌with‌‌the‌‌specified‌‌element‌‌
   ‌
void‌‌
trimToSize(‌‌
)  
‌‌‌
s‌ets‌‌the‌‌capacity‌‌of‌‌the‌‌list‌‌to‌‌its‌‌current‌   ‌ ‌
size‌  ‌ 54‌‌   ‌

Processing‌A
‌ rray‌L‌ ists‌‌   ‌
•‌U
‌ sing‌‌a‌‌standard‌f‌ or‌l‌oop:‌‌   ‌
ClassName‌‌
currentObject;‌‌‌
  
for‌‌
(‌‌
int‌‌
i‌‌
=‌‌
0;‌‌
i‌‌
<‌a
‌rrayListName.size(‌‌
);
‌‌‌
i++‌‌
 
)‌‌
{  
‌‌‌
currentObject‌‌
‌ =‌a
‌rrayListName.get(‌‌
i‌‌
);‌
  ‌
//‌‌
process‌‌
currentObject‌‌‌
  
}‌‌‌
  

•‌E‌ xample:‌‌   ‌
Auto‌‌
currentAuto;‌   
‌‌
for‌‌
(‌‌int‌‌
i‌‌
=‌‌
0;‌‌
i‌‌
<‌‌listOfAutos.size(‌‌
);‌‌
i++‌‌
 
)‌‌
{  
‌‌‌
c
‌urrentAuto‌‌
=‌‌
listOfAutos.get(‌‌
i‌‌
);‌
  ‌
//‌‌
process‌‌
currentAuto‌‌‌
  
}‌‌‌
  

55‌‌   ‌

The‌‌Enhanced‌f‌ or‌L‌ oop‌‌   ‌

•‌S‌ implifies‌‌processing‌‌of‌‌lists‌‌   ‌
•‌T‌ he‌‌standard‌‌form‌‌is:‌‌   ‌
for‌‌
(‌‌
ClassName‌‌
currentObject‌‌
:‌‌
arrayListName‌‌
)‌‌
{  
‌ ‌
//‌‌
‌ process‌‌
currentObject‌‌‌
  
}‌‌‌
  

•‌T‌ his‌e‌ nhanced‌f‌ or‌l‌oop‌p ‌ rints‌a‌ ll‌e‌ lements‌‌ 


of‌ a‌ n‌A‌ rrayList‌o
‌ f‌S‌ trings‌n
‌ amed‌l‌ist‌:  ‌‌ ‌
for‌‌
(‌‌
String‌‌
s‌‌
:‌‌
list‌‌
)  
‌‌
{‌‌‌
  
‌ystem.out.println(‌‌
S s‌‌);‌‌‌
  
}‌‌
 

56‌‌   ‌

ArrayList‌c‌ lass‌m
‌ ethods‌‌   ‌
•‌M
‌ ethods‌‌to‌‌add‌‌elements‌‌   ‌

–‌‌public‌b ‌ oolean‌‌add‌(‌ ‌‌Base_Type‌n ‌ ewElement‌)‌  ‌‌


•‌‌adds‌t‌ he‌n
‌ ew‌e‌ lement‌t‌ o‌t‌ he‌e‌ nd‌‌   ‌
•‌s‌ ize()‌‌increases‌‌by‌‌1  ‌‌ ‌
•‌‌capacity‌i‌ncreases‌i‌f‌n ‌ ecessary‌  ‌

57‌‌   ‌
ArrayList‌c‌ lass‌m
‌ ethods‌‌   ‌

•‌M
‌ ethods‌t‌ o‌a‌ dd‌e‌ lements‌‌   ‌

–‌p
‌ ublic‌‌void‌a‌ dd‌(‌ ‌i‌nt‌i‌ndex,‌B
‌ ase_Type‌n
‌ ewElement‌‌) ‌‌
•‌‌0<=index<=size()‌‌   ‌
•‌‌when‌i‌ndex==size(),‌i‌nserts‌a‌ t‌e‌ nd‌‌   ‌
•‌‌size()‌i‌ncreases‌b
‌ y‌1
‌   ‌‌ ‌
•‌‌capacity‌i‌ncreases‌i‌f‌n ‌ ecessary‌‌   ‌
•‌w‌ hen‌‌0<=index<size(),‌‌current‌‌elements‌‌at‌‌index‌  ‌
move‌t‌ o‌i‌ndex+1,‌a‌ t‌i‌ndex+1‌m ‌ ove‌t‌ o‌i‌ndex+2,‌…
‌ ,‌a‌ t‌  ‌
size()-1‌m
‌ ove‌t‌ o‌s‌ ize()‌  ‌
58‌‌   ‌

ArrayList‌c‌ lass‌m
‌ ethods‌‌   ‌

•‌M
‌ ethods‌‌to‌‌remove‌‌elements‌‌   ‌
–‌p
‌ ublic‌B ‌ ase_Type‌r‌ emove‌(‌ ‌i‌nt‌i‌ndex‌‌ 
)‌•‌ ‌‌0<=index<size()‌(‌ or‌e‌ xception)‌‌   ‌
•‌‌Removes‌e‌ lement‌a‌ t‌i‌ndex;‌s‌ hifts‌t‌ o‌t‌ he‌l‌eft‌  ‌
remaining‌‌elements‌‌at‌‌index+1‌‌…‌‌size()-1.‌‌   ‌
–‌p
‌ rotected‌‌void‌r‌ emoveRange‌(‌ ‌i‌nt‌f‌ romIndex,‌i‌nt‌t‌ oIndex‌‌) ‌‌
•‌‌Removes‌e‌ lements‌w
‌ ith‌i‌ndex‌i‌‌s‌ .t.‌   ‌ ‌
fromIndex<=i<toIndex;‌s‌ hifts‌t‌ o‌t‌ he‌l‌eft‌r‌ emaining‌  ‌
elements‌a‌ t‌t‌ oIndex‌…
‌ ‌s‌ ize()-1‌  ‌

59‌‌   ‌

ArrayList‌c‌ lass‌m
‌ ethods‌‌   ‌

•‌M
‌ ethods‌‌to‌‌remove‌‌elements‌‌   ‌
–‌p
‌ ublic‌b
‌ oolean‌r‌ emove‌(‌O
‌ bject‌t‌ heElement‌‌) ‌‌
•‌‌if‌f‌ ound‌t‌ hen‌r‌ emoves‌t‌ he‌fi
‌ rst‌o
‌ ccurrence‌o ‌ f‌  ‌
theElement;‌s‌ hifts‌t‌ he‌r‌ emaining‌e‌ lements‌t‌ o‌t‌ he‌ l‌eft;‌‌ 
size()‌‌becomes‌‌size()-1;‌‌returns‌‌true.‌‌   ‌
•‌‌if‌n
‌ ot‌f‌ ound‌t‌ hen‌r‌ eturns‌f‌ alse.‌‌   ‌
–‌p
‌ ublic‌‌void‌c‌ lear‌(‌ ‌‌)  ‌‌ ‌
•‌‌Removes‌a‌ ll‌e‌ lements;‌s‌ ize()‌b
‌ ecomes‌0
‌  ‌ ‌

60‌‌   ‌

ArrayList‌c‌ lass‌m
‌ ethods‌‌   ‌

•‌M
‌ emory‌‌management‌‌(size‌‌&‌‌capacity)‌‌ 
–‌p
‌ ublic‌‌boolean‌i‌sEmpty‌(‌ ‌‌)  ‌‌ ‌
•‌‌True‌i‌f‌e‌ mpty;‌f‌ alse‌o
‌ therwise.‌‌   ‌
–‌p
‌ ublic‌‌int‌s‌ ize‌(‌ ‌‌)  ‌‌ ‌
•‌‌Returns‌n ‌ umber‌o‌ f‌e‌ lements‌i‌n‌A‌ rrayList‌‌– ‌‌
public‌v‌ oid‌‌ensureCapacity‌‌(‌i‌nt‌n ‌ ewCapacity‌)‌ ‌•‌ ‌‌
 
Increases‌t‌ he‌c‌ apacity‌(‌ if‌n
‌ ecessary)‌‌   ‌
–‌p
‌ ublic‌‌void‌t‌ rimToSize‌(‌ ‌‌)  ‌‌ ‌
•‌‌Reduces‌c‌ apacity‌t‌ o‌c‌ urrent‌s‌ ize‌  ‌
61‌‌   ‌
62‌  ‌

Remember‌!‌ !!‌‌   ‌
when‌‌Copying‌‌Arrays‌‌   ‌

•‌S‌ uppose‌‌we‌‌want‌‌to‌‌copy‌‌the‌e‌ lements‌‌of‌‌an‌  ‌


array‌‌to‌a‌ nother‌‌array.‌‌We‌‌could‌‌try‌‌this‌‌code:‌  ‌
double‌‌
[]‌‌
billsBackup‌‌
=‌‌
new‌‌
double‌‌
[6];‌‌‌
  
billsBackup‌‌
‌ =‌‌
cellBills;‌‌
//‌‌
incorrect!‌‌‌
  

•‌A
‌ lthough‌‌this‌‌code‌‌compiles,‌‌it‌i‌s‌‌logically‌  ‌
incorrect!We‌‌are‌‌copying‌t‌ he‌‌cellBills‌o ‌ bject‌  ‌
reference‌‌to‌‌the‌‌billsBackup‌o
‌ bject‌r‌ eference.‌W ‌ e‌  ‌
are‌‌not‌c‌ opying‌‌the‌‌array‌‌data.‌   ‌ ‌
•‌T‌ he‌‌result‌‌of‌‌this‌‌code‌‌is‌‌shown‌‌on‌‌the‌‌next‌‌slide.‌  ‌
63‌‌   ‌

Copying‌A
‌ rray‌R
‌ eferences‌‌   ‌


billsBackup‌‌
=‌‌
cellBills;‌‌‌
  

h
‌ as‌‌this‌‌effect:‌  ‌
 ‌
64‌‌   ‌

Copying‌‌Array‌‌Values‌‌   ‌

•‌E‌ xample:‌‌this‌‌code‌‌copies‌‌the‌‌values‌‌of‌‌all‌  ‌
elements‌i‌n‌a‌ n‌a‌ rray‌n ‌ amed‌c‌ ellBills‌t‌ o‌a‌ n‌  ‌
array‌‌named‌b ‌ illsBackup‌,‌‌both‌‌of‌‌which‌‌ 
have‌ ‌previously‌‌been‌‌instantiated‌‌with‌‌the‌‌ 
same‌ l‌ength‌:  ‌‌ ‌
‌ or‌‌(‌‌int‌‌i‌‌=‌‌0;‌‌i‌‌<‌‌cellBills.length;‌‌i++‌‌)‌ ‌{  
f ‌‌‌
‌illsBackup[i]‌‌
b =‌‌
cellBills[i];‌
   
‌‌
}  
‌‌‌‌

•‌T‌ he‌e‌ ffect‌o


‌ f‌t‌ his‌f‌ or‌‌loop‌i‌s‌s‌ hown‌o
‌ n‌t‌ he‌‌ 
next‌ s‌ lide.‌  ‌
65‌‌   ‌

Copying‌A
‌ rray‌V
‌ alues‌‌   ‌
  ‌ ‌

66‌‌   ‌

Changing‌a‌ n‌A
‌ rray's‌S‌ ize‌‌   ‌
•‌A
‌ n‌‌array's‌l‌ength‌i‌nstance‌‌variable‌‌is‌  ‌
constant.‌‌   ‌
–‌t‌ hat‌‌is,‌‌arrays‌‌are‌‌assigned‌‌a‌‌constant‌‌size‌‌when‌  ‌
they‌‌are‌‌instantiated.‌   ‌ ‌
•‌T‌ o‌‌expand‌‌an‌‌array‌‌while‌‌maintaining‌‌its‌  ‌
original‌‌values:‌‌   ‌
1.‌‌Instantiate‌‌an‌‌array‌‌with‌‌the‌‌new‌‌size‌‌and‌‌a ‌  ‌ ‌
temporary‌‌name.‌‌   ‌
2.‌‌Copy‌‌the‌‌original‌‌elements‌‌to‌‌the‌‌new‌‌array.‌‌   ‌
3.‌‌Point‌‌the‌‌original‌‌array‌‌reference‌‌to‌‌the‌‌new‌‌array.‌‌   ‌
4.‌‌Assign‌‌a‌n
‌ ull‌v‌ alue‌‌to‌‌the‌‌temporary‌‌array‌‌reference.‌67‌‌   ‌

Expanding‌‌the‌‌Size‌‌of‌‌an‌‌Array‌‌   ‌

•‌T‌ his‌‌code‌‌will‌‌expand‌‌the‌‌size‌‌of‌‌the‌‌ 
cellBills‌a‌ rray‌‌from‌‌6‌‌to‌‌12‌‌elements:‌‌   ‌
‌ /instantiate‌‌new‌‌array‌‌‌
/   
‌ouble‌‌
d []‌‌
temp‌‌
=‌‌
new‌‌
double‌‌
[12];‌
   
‌‌
   ‌‌
//‌‌
‌ copy‌‌all‌‌
elements‌‌
from‌‌
cellBills‌‌
to‌‌temp‌ f
‌or‌‌

‌‌
int‌‌ i‌‌
=‌‌
0;‌‌
i‌‌
<‌‌
cellBills.length;‌‌
i++‌‌)‌ {
‌  
‌‌‌
‌emp[i]‌‌
t =‌‌
cellBills[i];‌‌
//‌‌
copy‌‌
each‌‌
element‌ ‌
}  
‌‌
   ‌‌
//‌‌
‌ point‌‌
cellBills‌‌
to‌‌
new‌‌
array‌‌‌
  
cellBills‌‌
=‌‌
temp;‌
   
‌‌
temp‌‌
‌ =‌‌
null;‌‌
  
68‌‌   ‌

Implementing‌‌the‌‌Add‌‌method‌‌in‌‌ 
array‌ ‌List‌   ‌ ‌
69‌  ‌

Implementing‌‌the‌‌Remove‌‌method‌‌in‌  ‌
array‌‌List‌   ‌ ‌
‌ 70‌  ‌

Implementing‌‌the‌‌Remove‌‌method‌‌in‌  ‌
array‌‌List‌‌at‌‌specific‌‌index‌‌i????‌‌   ‌

Homework‌   ‌ ‌

71‌  ‌

You might also like