Comments (8)
Hi André! Thank you so much for commenting on this code.
I think the solution I have works as is.
public void delete(int index) {
// Copy down
for (int j = index; j < size - 1; j++) {
data[j] = data[j + 1];
}
size--;
// Clear if last element in array
if (index == size) {
data[index] = null;
}
}
Because I decrement size--
before doing the check, I can delete the last element OK.
So if we look at this example.
0 1 2 size = 3
a b c
delete(2)
copy down
0 1 2 size = 3
b c c index = 2
We now have an extra c at index = 2 that we want to delete.
If we decrement the size--
0 1 2 size = 2
b c c index = 2
We can delete the last element if the index == the new size or our array.
if (index == size) { // 2 = 2
data[index] = null;
}
0 1 2 size = 2
b c null index = 2
Does this make sense? I may have missed something, but I think it works.
If it doesn't, can your write a unit test that shows me where the bug is? I can then try to fix.
Thank you so much for raising this question and asking.
Cheers - Jonathan
from algorithms101.
Hi Jonathan, tanks fos answer :)
I'm actually writing unit tests for the exercises
I'll follow your suggestion and check by the tests.
from algorithms101.
It doesn't work.
public void delete(int index) {
// Copy down
for (int j = index; j < size - 1; j++) {
data[j] = data[j + 1];
}
size--;
// Clear if last element in array
if (index == size) {
data[index] = null;
}
}
@Test
public void shouldDeleteFromArray(){
var array = new DynamicArray<>(2);
array.add("object1");
array.add("object2");
array.add("object3");
array.delete(1);
assertThat(array.size(), is(2));
assertThat(array.get(0), is("object1"));
}
By my sugestion:
from algorithms101.
Which assertion in your unit test is failing?
from algorithms101.
The unit tests are passing. But the final array still holding dirty data in last index.
from algorithms101.
Apologies :) I understand now. Bad unit test on my part.
Yes you are correct! And you solution works! Thank you.
I am going to tweak it a bit and add some comments.
Thanks again for pointing this out. You are really getting this stuff!
Cheers - Jonathan
from algorithms101.
Note: I gave you credit for finding the but on the page that explains what the bug is. Let me know if you if that is OK.
Cheers - Jonathan
from algorithms101.
Hi @jrasmusson
I'm pretty happy to discuss with you that solution.
Congratulations! The course is awesome.
from algorithms101.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from algorithms101.