ArrayList question.

This is a discussion on ArrayList question. within the C# Programming forums, part of the General Programming Boards category; Could anyone please explain how to use Arraylists? And is this really the only way to store info dynamically? if ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    ArrayList question.

    Could anyone please explain how to use Arraylists? And is this really the only way to store info dynamically? if so I think I'll miss malloc. The reason I need to allocate memory dynamically is I'm trying to make a program which asks the user for any number of integers. At first they are stored in a string using "string input = Console.Readline()", after that I use a for-loop to detect the actual integers and try sorting out other letters and spaces that the user could have entered by mistake. This is the point where I need to store the integers dynamically while converting them from the string. And I know about the split() method but I think it's good to practice myself.

    I would really appreciate all suggestions and help I can get!

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    As far as I know there is no other way to have a dynamic array in C#, unless you do something like

    - create normal 1d array of ints
    - check wether last value of the array is not null
    if so
    copy complete array into a bigger array
    do some garbage collection
    - else
    continue adding values

    The only major problem with an ArrayList is the boxing of the ints to objects, and when reading and converting to ints from the arraylist the unboxing.

    You can easily test performance of an ArrayList vs a normal array by timing the operation like this

    start timer for arraylist
    create arraylist
    store a bunch of ints or whatever in the arraylist
    retrieve them again from the arraylist
    stop timer for arraylist

    now do the same for a normal array and see the difference, most of the time needed to add / retrieve values from an ArrayList goes into the boxing/unboxing...

    edit: a quick solution to your problem is to do something like
    Code:
    int []ar;
    string input = Console.ReadLine();
    ....
    //go over string and check how many ints there are -> store result in "numberOfInts"
    ar = new int[numberOfInts];
    //go again over the whole string and add ints to the array
    But as you can see ... its somehow messy...
    Last edited by GanglyLamb; 05-17-2006 at 01:36 PM.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    If you're using the 2.0 framework, you can use a generic List instead of the ArrayList. The generic List doesn't suffer the same performance penalties as the ArrayList with boxing/unboxing. Further, all access of the generic List is strongly typed, not object references that you have to cast every time.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Thanks for your replies, I choose GanglyLambs technique with the 2 loops, and it works fine.

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Well if you are using framework 2.0 I would go for the approach that pianorain suggested.

    Didnt even know myself about these Generic Lists ( but now I do ), I looked into it briefly and it would be far better then using the dirty way with the two loops... ( that is if you are writing for the 2.0 ).

    But then again you are just doing this as an exercise to implement your own split method , using "dynamic" arrays , so I it really doesnt matter anyway ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21