Results 1 to 7 of 7
  1. #1
    Sophiie is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default 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!!!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    I dont understand them
    What do you think increaseSize would do? If the current size is 10, and you used increaseSize(10) then the new size would be 20.

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    purpose of copying a list to an array, using a list we can do almost all process doing on an array
    I think his assignment is to write a list class.
    Internally some container classes use arrays. His assignment probably does.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    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.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •