Thread: Arraylist methods

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Arraylist methods

    I have class arraylist with all basic procedures (checkIndex, get, insert, erase nad output).
    In private or protected part i have:
    Code:
    T* element  //1D field that contains elements of list
    int arrayLength  //size of field
    int listSize  //number of elements in list
    I have to add two methods in class:

    • arrayList<T>::removeRange ( remove elements on given interval. Interval has to be passed with inital and final index)
      Example: x=[1,2,3,4,5] //list
      x.removeRange(1,3) //call method
      x=[1,5] //result
    • arrayList<T>::swap(i,j)
      (replace elements on indexes i and j)
      Example: x=[1,2,3,4,5] //list
      x.swap(1,3) //call method
      x=[1,4,3,2,5] //result


    I try to add those two methods but without success
    I don't know what to write in parenthesises beside method, which two variables should I use? Can I just come up with new two of them or have I use already existent?


    I also attached how i try to write one method. I don't know if this is right type of thinking or should i think in different way.
    Every opinion and help is more than wellcome
    Last edited by VirtualAce; 09-15-2008 at 04:48 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Edited for clarity.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well you officially have the cutest name for a new member. I digress. Why not use iterators?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    element[i] has type T
    so I suppose the temp var should be of that type as well
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I think you can only use templates in header files..

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think you're trying to make swap do too much. It shouldn't involve any loops, temp should be of type T, and you need to use the input parameters a and b. You could also do range checking if you want to be tidy.
    Last edited by iMalc; 09-16-2008 at 12:58 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 02:52 AM
  2. ArrayList Basics - Example
    By wbeasl in forum C# Programming
    Replies: 2
    Last Post: 12-26-2008, 04:41 PM
  3. Trouble using ArrayList
    By stevespai in forum C# Programming
    Replies: 12
    Last Post: 07-31-2006, 11:17 AM
  4. Lesson #5 - Methods
    By oval in forum C# Programming
    Replies: 1
    Last Post: 05-04-2006, 03:09 PM
  5. Replies: 8
    Last Post: 07-27-2003, 01:52 PM