Jump to content

c# Array Lists

- - - - -

  • Please log in to reply
2 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
C# Array Lists

Arrays are useful for when your data is of a fixed size. But when you have a need for a data structure that grows over time you can make use of an array list. An array list is a structure built using an array but has the ability to change it sizes as items are added and removed.
This structure is particularly handy in programs using databases as you never know how many results you will get back from a select query.

Creating an Array List

To create an array list you simply write a statement like this:

ArrayList items = new ArrayList();
 

This creates an ArrayList that can hold items of any type. You can add integers, doubles, chars, strings, anything you want. It is important to note that if you add a primitive type to the ArrayList it is actually converted to an equivalent object wrapper.
Note: the ArrayList class does not support the use of generics. If this is something you want look at the List class instead.

Adding Items

To add items we can use the Add method:

items.Add(3);
 items.Add(“codecall”);

We can also add an entire collection to the end of a list using the AddRange function:

items.AddRange(new []{1,3,2,4});

Note: there is no function to add a collection to the front of a list. However, this is possible by making a second list with just the elements you want at the front and then adding the original list to the end of that list as follows:


ArrayList items = new ArrayList();
 
            items.Add(5);
            items.Add(3);
 
            items.AddRange(new []{1,3,2,4});
 
            var items2 = new ArrayList() {0, 0, 0};
            // add items2 to the front of items
            items2.AddRange(items);
            items = items2;
 
            foreach (var itms in items) 
                Console.WriteLine(itms);

We add all of the elements in items to the end of items2. So now items2 has the representation {0, 0, 0, 5, 3, 1, 3, 2, 4} and items has the representation {5, 3, 1, 3, 2, 4}. This, however, is not the result we want. We want items to have the representation {0, 0,0, 5, 3, 1, 3, 2, 4}. This is why we set the reference of items to be the same as items2. Now the foreach loop will output the contents of items as {0, 0, 0, 5, 3, 1, 3, 2, 4} as we wanted.

Removing Items

There are three ways of removing items from an array list:

  • Remove
  • RemoveAt
  • RemoveRange
Remove simply takes an object that you want to remove and removes the first occurrence of it.

Example:

items.Remove(3);

This removes the first occurrence of 3 from items and the list becomes {0, 0, 0, 5, 1, 3, 2, 4}. If I call Remove with a parameter that does not exist the function simply does nothing.

RemoveAt takes an index and removes the element at the index.

Example:

Items.RemoveAt(0); // removes first element in the array list

It is important remember that array lists start at index 0 not 1 just like arrays.
This function DOES NOT check for out of bounds errors... so to be perfectly safe you should include a check to make sure the index is in the bounds of the array before calling the function.
RemoveRange takes two parameters: the index of the first element to remove followed by the number of elements to remove from the collection. Like RemoveAt this function does not do bounds checking.

As usual, any questions feel free to ask. :)

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Nice. What other kinds of non primitive data structures does C# offer?

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
The System.Collection namespace has quite a few data structures in it.

BitArray (an array of bytes represented by boolean variables). I suppose this is basically a boolean array wrapper.

The Concurrent namespace has the following:

ConcurrentBag
ConcurrentStack
ConcurrentQueue
ConcurrentDictionary

In the System.Collections.Generic namespace we have the following:
LinkedList
Queue
Stack
List
SortedList
SortedDictionary
SortedSet
HashSet

What is going to be interesting is seeing what they are calling a list? My assumption is that it is a generic implementation of array list but not sure of that just yet.

The Generic namespace also has a LinkedListNode class in it.

In the System.Collections namespace is a Hashtable class.

I do plan on writing about more of these soon.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users