~ Mansukh
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Mansukhdeep Thind wrote:How will you go about creating a growable array which can do all the things an ArrayList can?
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
It is very well possible to write basic ArrayList implementation using Array class.
~ Mansukh
Which Array class? In the java.lang.reflect package?
How is that going to help? Or do you mean by using the concept of creating a generic Object[] array?
How does one make an array grow dynamically as and when elements are added to it? In Java language, arrays are defined as containers with a fixed size.
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
~ Mansukh
I think it is the less severe of the two errors I can seeMansukhdeep Thind wrote:. . . What is the "sizeratio" for? . . .
Hmm.. I am trying without referring to any other sources Ritchie. Why do you term this as an error? We have to start with some size. Then as we add elements, we have to increase the size proportionately. Correct?I think it is the less severe of the two errors I can see
~ Mansukh
Oracle certified JPA Developer (1Z0-898),Oracle certified Java 8 Programmer I (1Z0-808), Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Abhay Agarwal wrote:sizeratio is number by which arraylist size is initialized.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
@Mansukhdeep: Unfortunately, we don't really have enough information about this interview question: ie, Precisely what they wanted your "CustomArrayList" class to be able to do.
I believe I need to re-size it whenever I am adding a new element. Correct?all you need to know is when you need to resize it.
The real problem is that ArrayList implements the List interface; and that has a pile of stuff - including two types of Iterator - that are likely to present you with a lot more problems than just the business of when and how to resize an array
~ Mansukh
Mansukhdeep Thind wrote:I believe I need to re-size it whenever I am adding a new element. Correct?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mansukhdeep Thind wrote:I believe I need to re-size it whenever I am adding a new element. Correct?
Not at all. The code does not have anything in which needed synchronisation. The errors I saw were much simpler. Real “beginning Java” stuff.Abhay Agarwal wrote:. . .
My take is that you are probably referring to synchronization issues in my above code.
Apart from what Winston has already told you, and the poor formatting of the name: why on earth do you want a static field with the initial size at all? It is something which is only used locally in the constructor, so why not defy the style suggestion about magic numbers and simply write 10. It should be a local variable in the constructor. Add a comment explaining that you are initialising it to 10 elements. And, if you overload your constructor, make sure to do that correctly, otherwise I shall find another error.sizeratio is number by which arraylist size is initialized.
~ abhay
Adding an element would be an O(N) operation, whereas in the existing ArrayList it's O(1). If I were the interviewer, I'd deduct points for that.
~ Mansukh
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:I believe I need to re-size it whenever I am adding a new element. Correct?
That's one approach you could take. It would lead to poor performance though...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mansukhdeep Thind wrote:
Adding an element would be an O(N) operation, whereas in the existing ArrayList it's O(1). If I were the interviewer, I'd deduct points for that.
What is meant by O(N) operation and O(1) operation?
What functions are you referring to? The time it takes to add the element?
Wikipedia link.Mansukhdeep Thind wrote: . . . What is meant by O(N) operation and O(1) operation? . . .
Jeff Verdegan wrote:O(1) means it's a constant-time operation. Adding an element doesn't depend on the size of the list.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Jeff Verdegan wrote:O(1) means it's a constant-time operation. Adding an element doesn't depend on the size of the list.
<nitpick>
~ Mansukh
~ Mansukh
Mansukhdeep Thind wrote:OK Jeff. Will put in some effort in this direction.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
~ Mansukh
~ Mansukh
Mansukhdeep Thind wrote:
It gives the output:
x before grow: [A, B, C, D]
x after grow: [A, B, C, D, null, null, null, null, null]
Is this what you were expecting of me?
The main thing though is that you embrace the technique of writing a small class with a small number of methods to work out how to solve the current problem.
~ Mansukh
I wouldn't bother passing the array to the method. That method is just meant to work on one specific, well-known member variable. It's not for growing an arbitrary array that's passed to it.
Taken care of.I would also do away with passing the new size. I would just calculate it on the fly based on the size of the original array. However, if you have some other approach to computing the new size, it may be appropriate for you to pass it.
Done.And finally, growableArray should not be a member variable. It's a temporary variable that's only used during the execution of that method, so it should be local to that method.
Also done.Once you get the above method working, you will place it into your ArrayList class. And I don't mean just copy the body of the method and stick it inside your add() method inline. I mean copy the whole method.
~ Mansukh
No you didn’t. There is no such thing. What you are doing is replacing it with a larger array. Its implications have already been discussed.Mansukhdeep Thind wrote:I succeeded in implementing a dynamically growable array.
I am please to see you have escaped the more serious of the two errors I mentioned earlier. And I haven’t yet told anybody what it is.. . .
OK. I did a copy. I take back my words.What you are doing is replacing it with a larger array.
Why are you passing the list name to the add method?
~ Mansukh
Things sound so simple when we first learn the basic concepts. But implementing those concepts wisely and in a fruitful manner when faced with a real time scenario is a whole different ball game.You already have an instance of the class to call it on. Sometimes called this.
~ Mansukh
Things are so simple when you implement them correctlyMansukhdeep Thind wrote: . . . Things sound so simple when we first learn the basic concepts. . . .
Without even reading the code … no. You want to get the first part working correctly before you try any enhancements. Get one method working before you even think about the second method.
Am I on the right track?
~ Mansukh
Your get method is unnecessarily complicated. You are still using null== and null!= which shouldn’t be used in Java.
Start by implementing size(), add(E) add(int, E) get(int) set(int, E) and remove(int).
OK.Forget about the iterator until you have got all that lot working.
You will need a modificationCount field, and use that to check concurrent modification, so you can throw an Exception from the Iterator as appropriate.
I suggest you start by parameterising the class completely, so it is
OK.You will not un‑comment those interfaces until you have implemented a lot more methods.
You do not need initialSize as a field.
~ Mansukh
Nothing up my sleeve ... and ... presto! A tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|