Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
f2485612
Commit
f2485612
authored
9 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Reallocation by "copyOf"
Renaiming variable to initialCapacity for the sake of better comprehension
parent
8db8d936
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
P/Sd1/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java
+5
-7
5 additions, 7 deletions
...main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java
with
5 additions
and
7 deletions
P/Sd1/Array/integerStoreUnbounded/src/main/java/de/hdm_stuttgart/mi/sd1/store/IntegerStore.java
+
5
−
7
View file @
f2485612
package
de.hdm_stuttgart.mi.sd1.store
;
import
java.util.Arrays
;
/**
* A container holding a fixed
* number of integer values.
...
...
@@ -7,7 +9,7 @@ package de.hdm_stuttgart.mi.sd1.store;
*/
public
class
IntegerStore
{
final
static
int
default
Capacity
=
4
;
final
static
int
initial
Capacity
=
4
;
long
[]
values
;
// Array containing our values
int
numValues
=
0
;
// Number of values present in the container so far.
...
...
@@ -17,7 +19,7 @@ public class IntegerStore {
*
*/
public
IntegerStore
()
{
values
=
new
long
[
default
Capacity
];
values
=
new
long
[
initial
Capacity
];
}
/**
* Create a new store being able to
...
...
@@ -56,11 +58,7 @@ public class IntegerStore {
*/
public
void
addValue
(
long
value
)
{
if
(
values
.
length
<=
numValues
)
{
// Sufficient capacity available to add a new value?
final
long
[]
currentArray
=
values
;
values
=
new
long
[
2
*
currentArray
.
length
];
// Double the current array's size.
for
(
int
i
=
0
;
i
<
currentArray
.
length
;
i
++)
{
values
[
i
]
=
currentArray
[
i
];
}
values
=
Arrays
.
copyOf
(
values
,
2
*
values
.
length
);
// Double the current array's size.
}
values
[
numValues
++]
=
value
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment