Results 1 to 7 of 7
Thread: increaseSize and addElement help
- 11-07-2008, 03:53 AM #1Member
- Join Date
- Nov 2008
- Rep Power
increaseSize and addElement help
Im currently working on a project that asks for:
It is often necessary to add items to or remove items from a list. When the list is stored in an array, one way to do this is to
create a new array of the appropriate size each time the number of elements changes, and copy the values over from the old
array. However, this is rather inefficient. A more common strategy is to choose an initial size for the array and add elements
until it is full, then double its size and continue adding elements until it is full, and so on.
1. Add this capability to the IntegerList class. You will need to add an increaseSize method plus instance variables to hold
the current number of integers in the list and the current size of the array. Since you do not have any way to add elements
to the list, you won't need to call increaseSize yet.
2. Add a method void addElement(int newVal) to the IntegerList class that adds an element to the list. At the beginning of
addElement, check to see if the array is full. If so, call increaseSize before you do anything else.
I tried googling both things but I dont understand them. Can anyone explain to me what they are for, more precisely, and how do I use em.
If possible, can anyone show me examples of how to use em.
Also, I need help on how to insert in into the the IntegerList class that can be found on the link below.
The program Im working on can be found here: cs.roanoke.edu/~cpsc/AW/labs/ch6/IntegerListII.html
Thank you anyone who helps!!!
- 11-07-2008, 04:31 AM #2I dont understand them
There are currently addElement methods in Java's API doc. Use the Index at the top right of the API doc page, select A and scan down to the addElement methods and read about them.
- 11-07-2008, 04:58 AM #3
I wonder on one thing here. What's the purpose of copying a list to an array, using a list we can do almost all process doing on an array. Because to me, even double the size time to time is inefficient. Think initially array has 100 elements, once you get the 101th element the size is 200. After that if you only added 5 elements what happen to the rest of 95 memory locations. Think about it in larger scale.
- 11-07-2008, 06:24 AM #4purpose of copying a list to an array, using a list we can do almost all process doing on an array
Internally some container classes use arrays. His assignment probably does.
- 11-07-2008, 06:37 AM #5
Fine. But the way he's working on, I mean increasing the size of an array, is not good for me. For a simple class assignment it's fine, but for large scale project it's dead.
- 11-07-2008, 03:08 PM #6
I think this is a class assignment. For large scale, there could be some tuning to be done for the initial size of the internal array and the increment sizes to minmize system impact for a specific environment or application.
- 11-07-2008, 06:10 PM #7
I agreed with you Norm, since this is not a large scale application the way going to workout here is not an issue. At the same time, I think it's better if we can deal with the best way at all. ;) That's my thoughts lol.