There are three kinds of actuaries: those who can count, and those who can't.
Matthew Bendford wrote:Take a look at Vector vs ArrayList as an example:
Matthew Bendford wrote:Vector was designed very conservative - on every enlargement its size doubled - starting with a vector with its size equal to a power of two you can see this gets really big rather quick. ArrayList on the other hand was done more smarter: It only increases the backing array only by half its current size. A list with an initial size of 8 would go 12, then 18, then 27 and so on - a vector would already at 32 or even at 64.
Matthew Bendford wrote:Sure, you can go "+1" each step - but that's not really an algorithm - but rather a very compilcated way of implementing a LinkedList - which doesn't follow any enlargement algorithm but just chains the next element after the last one and hence also only growing by +1 each time.
Jason De Vere wrote:But I'm learning algorithms :-)
There are three kinds of actuaries: those who can count, and those who can't.
That code doesn't copy the array, but only its reference, causing aliasing problems. Any changes to array1 are reflected in temp, because the two point to the same objectPiet Souris wrote:. . .. . .
I suspect the two will average themselves out so there isn't actually a difference. Apart from the fact that all ArrayList operations run faster than Vector's because there is no synchronisation overhead.Mike Simmons wrote:It's not really clear to me which of these is better.
Campbell Ritchie wrote:That code doesn't copy the array, but only its reference, causing aliasing problems. Any changes to array1 are reflected in temp, because the two point to the same object
Campbell Ritchie wrote:
And, JDV, welcome to the Ranch (again)
Campbell Ritchie wrote:A straight copy of references wouldn't enlarge the array.
OP wrote:
Jason de Vere wrote:But I'm learning algorithms :-)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
The author, Martin Fowler wrote:In our olfactory analogy, comments aren’t a bad smell; indeed they are a sweet smell. The reason we mention comments here is that comments often are used as a deodorant. It’s surprising how often you look at thickly commented code and notice that the comments are there because the code is bad… Our first action is to remove the bad smells by refactoring. When we’re finished, we often find that the comments are superfluous.
Jason de Vere wrote:I have tested the final result and it's working.
Winston Gutkowski wrote:I think you're doing fine, and I particularly like the fact that you're explaining each step in your code. You'd be amazed how many experienced programmers forget to do that.
I also like the fact that you've named your method expand(), because that's exactly what it's doing. It's also a verb, and methods are usually actions of some sort.
However, what I don't like is that your method takes no parameters and returns no value. This makes it highly "coupled" - meaning that it doesn't stand on its own.
If you look at Junilu's example, his method takes the original array as a parameter and spits back a "modified" array, so the only thing left to do is assign it somewhere. And his code never references anything outside the method.
This is called "re-usability" and means you can - for example - copy and paste it into another program to do something similar, or (probably better) add it to a static library of generic "Array" methods, which is precisely what java.util.Arrays is.
As you go forward, what you'll discover is that most methods follow the "IPO" pattern - IPO standing for "Input - Process - Output".
That is: they take an input (in your case, an array), perform some sort of process on it (in your case enlarging it), and produce a result (the expanded array).
So the general signature pattern for most methods is:
{public/protected/private} final return-value method-name(parameter1, [parameter2, ...])
And don't forget that "final", even though it's redundant for private methods. Just get into the habit of adding it, because one of these days it will save your bacon. :-)
HIH
Winston
Jason de Vere wrote:At the moment I'd doing a course in Algorithms and Datastructures, where most of the time we are discussing mathematical concepts and using pseudocode. The lecturers advise us to use C++ to implement it if we want to try it out. However, I haven't got a clue about C++ so I was doing it with Java. That is why I am avoiding using any advanced methods.
Junilu Lacar wrote:The CS curriculum (over 30 years ago now) I went through had us learn a programming language first before we studied algorithms and data structures. That is, CS101 - Computer Programming (in Pascal) was a pre-requisite for CS204 Algorithms and Data Structures (in Pascal). It seems it would be difficult to learn about the latter if you didn't already have some basic mastery of the implementation language you were using.
Junilu Lacar wrote:
Jason de Vere wrote:At the moment I'd doing a course in Algorithms and Datastructures, where most of the time we are discussing mathematical concepts and using pseudocode. The lecturers advise us to use C++ to implement it if we want to try it out. However, I haven't got a clue about C++ so I was doing it with Java. That is why I am avoiding using any advanced methods.
There's not much difference in C++ and Java as it relates to writing a program like this. The implementation language is not that important here. It's how you organize the ideas that make up your solution that's crucial. If you're studying algorithms and data structures then sure, you're going to have build almost everything yourself instead of using parts/assemblies that are already available for you to just use.