Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs2-23wi
lectures
Commits
eb522d83
Commit
eb522d83
authored
2 years ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Code after lecture04
parent
10098307
master
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
04/ArrayListClient.java
+26
-0
04/ArrayListClient.java
04/IList.java
+10
-0
04/IList.java
04/OurArrayList.java
+85
-0
04/OurArrayList.java
with
121 additions
and
0 deletions
+121
-0
04/ArrayListClient.java
0 → 100644
View file @
eb522d83
import
java.util.ArrayList
;
import
java.util.TreeSet
;
public
class
ArrayListClient
{
// Set<String> set = new TreeSet<>();
public
static
void
main
(
String
[]
args
)
{
IList
<
String
>
list
=
new
OurArrayList
<>();
for
(
int
i
=
0
;
i
<
19
;
i
++)
{
list
.
add
(
""
+
i
+
"!"
);
}
System
.
out
.
println
(
list
);
/*
for (String s : list) {
}
*/
/*
int[] =======> [int][int][int].....
Object[] ====> [ | ][ | ][ | ]
v v v
*/
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
04/IList.java
0 → 100644
View file @
eb522d83
public
interface
IList
<
E
>
{
public
int
size
();
public
void
set
(
int
idx
,
E
elem
);
public
E
get
(
int
idx
);
public
void
clear
();
public
void
add
(
int
idx
,
E
elem
);
public
void
add
(
E
elem
);
}
This diff is collapsed.
Click to expand it.
04/OurArrayList.java
0 → 100644
View file @
eb522d83
public
class
OurArrayList
<
E
>
implements
IList
<
E
>
{
private
E
[]
data
;
private
int
size
;
// size vs. capacity
// current size = 0
// max size (capacity) = 10
public
OurArrayList
()
{
this
.
data
=
(
E
[])
new
Object
[
10
];
this
.
size
=
0
;
}
//[1, 2, 3]
public
String
toString
()
{
String
elements
=
""
;
for
(
int
i
=
0
;
i
<
this
.
size
;
i
++)
{
elements
+=
this
.
data
[
i
]
+
", "
;
}
if
(
elements
.
length
()
>
0
)
{
elements
=
elements
.
substring
(
0
,
elements
.
length
()
-
2
);
}
return
"["
+
elements
+
"]"
;
}
/**
* Puts element at index idx, shifting over everything after to the right.
* @param idx
* @param element
* For example, idx = 2, element = 42
* [1, 2, 3, 4, 5]
* ^
* [1, 2, 42, 3, 4, 5]
*/
public
void
add
(
int
idx
,
int
element
)
{
}
@Override
public
int
size
()
{
return
this
.
size
;
}
@Override
public
void
set
(
int
idx
,
E
elem
)
{
if
(
idx
<
0
||
idx
>
this
.
size
)
{
throw
new
IllegalArgumentException
(
"blah"
);
}
else
if
(
idx
==
size
)
{
add
(
elem
);
}
this
.
data
[
idx
]
=
elem
;
}
public
E
get
(
int
idx
)
{
return
null
;
}
@Override
public
void
clear
()
{
this
.
size
=
0
;
}
public
void
add
(
E
elt
)
{
if
(
this
.
size
==
this
.
data
.
length
)
{
E
[]
newData
=
(
E
[])
new
Object
[
this
.
size
*
2
];
for
(
int
i
=
0
;
i
<
this
.
data
.
length
;
i
++)
{
newData
[
i
]
=
this
.
data
[
i
];
}
this
.
data
=
newData
;
}
this
.
data
[
size
]
=
elt
;
this
.
size
++;
}
@Override
public
void
add
(
int
idx
,
E
elem
)
{
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help