



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




AP Computer Science
Arrays in Java
1
AP Computer Science
Arrays in Java
integer expression
. Due to dynamic allocation does not
have to be a constant.)
AP Computer Science
Arrays in Java
3
AP Computer Science
Arrays in Java
intList
double[]
dList
String[]
sList
=^ {"Olivia",
"Kelly",
"Isabelle"};
AP Computer Science
Arrays in Java
5
void
objectArrayExamples()
{^
Rectangle[]
rectList
new^
Rectangle[10];
//^ How
many
Rectangle
objects
exist?
rectList[5].setSize(5,10);//uh
oh! for(int
i^ =
i^ <
rectList.length;
i++)
rectList[i]
=^ new
Rectangle();
} rectList[3].setSize(100,200); }
AP Computer Science
Arrays in Java
6
AP Computer Science
Arrays in Java
7
AP Computer Science
Arrays in Java
8
AP Computer Science
Arrays in Java
13
AP Computer Science
Arrays in Java
AP Computer Science
Arrays in Java
15
AP Computer Science
Arrays in Java
AP Computer Science
Arrays in Java
17
AP Computer Science
Arrays in Java
AP Computer Science
Arrays in Java
19
AP Computer Science
Arrays in Java
AP Computer Science
Arrays in Java
25
public
void
removeAllLength(ArrayList
li,
int
len)
//pre:
li
contains
only
String
objects
//post:
all
Strings
of^
length
=^ len
removed
//right
way String
temp; for(int
i^
i^ <
li.size();
i++)
temp
=^ (String)li.get(i); if(^
temp.length()
len
li.remove(i);i--; } } } What if the list contains ["hi", "ok", "the", "so", "do"] and len = 2?
AP Computer Science
Arrays in Java
public
void
removeAllLength(ArrayList
li,
int
len)
//pre:
li^
contains
only
String
objects
//post:
all
Strings
of
length
=^ len
removed
//right
way
using
iterator
String
temp; iterator
it
=^ li.iterator();
while(
it.hasNext()
temp
=^ (String)li.next(); if(^
temp.length()
len
it.remove();
} } What if the list contains ["hi", "ok", "the", "so", "do"] and len = 2?