From Array To Arraylist
Written by: maffelu , 2009-09-08 18:06:09
Item[] items = new Item[3];
items[0] = new Item(1);
items[1] = new Item(2);
items[2] = new Item(3);
items[3] = new Item(4); //ERROR: No room for this one
Item[] items = new Item[3];
items[0] = new Item(1);
items[1] = new Item(2);
items[2] = new Item(3);
//Create a temporary array with the new size
Item[] temp = new Item[items.Length + 1];
for (int i = 0; i < items.Length; i++) {
//Copy the old array to the temporary one
temp[ i] = items[ i];
}
//Copy over the temporary array with a new size
//to the old array again.
items = temp;
//Add item
items[3] = new Item(4);
public class ItemList{
private Item[] _items;
public void Add(Item item) {}
}
public class ItemList{
private Item[] _items;
public int Size {
get { return this._items.Length; }
}
public void Add(Item item) {}
public int Count() {
int elements = 0;
for (int i = 0; i < this.Size; i++) {
if (this._items[ i] != null) {
elements++;
}
}
return elements;
}
}
public class ItemList{
private const int _defaultSize = 5;
private Item[] _items;
public int Size {
get { return this._items.Length; }
}
public ItemList(){
//Initate and set a default size for the array
this._items = new Item[_defaultSize];
}
public void Add(Item item) {}
public int Count() {
int elements = 0;
for (int i = 0; i < this.Size; i++) {
if (this._items[ i] != null) {
elements++;
}
}
return elements;
}
}
class Program{
static void Main(string[] args){
ItemList items = new ItemList();
Console.WriteLine("ItemList count: {0}", items.Count());
Console.Read();
}
}
public class ItemList{
private const int _defaultSize = 5;
private Item[] _items;
public int Size {
get { return this._items.Length; }
}
public ItemList(){
//Initate and set a default size for the array
this._items = new Item[_defaultSize];
}
public int Count() {
int elements = 0;
for (int i = 0; i < this.Size; i++) {
if (this._items[ i] != null) {
elements++;
}
}
return elements;
}
public void Add(Item item) {
if (Count() == this.Size) {
//Create a new, temporary, array to hold the old values
Item[] tempArray = new Item[this.Size];
//Copy the old values to the temporary array
for (int i = 0; i < this.Size; i++) {
tempArray[ i] = this._items[ i];
}
//Create a new size by doubling the old size
int newSize = this.Size + this.Size;
//Create a new array with the new and improved size
this._items = new Item[newSize];
//Copy back the old items to the new and improved array
for (int i = 0; i < tempArray.Length; i++) {
this._items[ i] = tempArray[ i];
}
}
//Add the new item
int numberOfElements = Count();
this._items[numberOfElements] = item;
}
}
class Program{
static void Main(string[] args){
ItemList items = new ItemList();
items.Add(new Item(1));
items.Add(new Item(2));
items.Add(new Item(3));
items.Add(new Item(4));
Console.WriteLine("ItemList count: {0}", items.Count());
//ItemList count: 4
Console.Read();
}
}
public Item this[int index] {
get {
if ((index < 0) || index >= this.Size) {
throw new ArgumentOutOfRangeException();
}
return this._items[index];
}
}
public void RemoveAt(int index) {
//Create a temporary array
Item[] temp = new Item[this.Size - 1];
int count = 0;
//Loop through the array and copy the elements
for (int i = 0; i < this.Size; i++) {
//Don't copy if it is the element we want to remove
if ((i != index)) {
temp[count] = this._items[ i];
count++;
}
}
this._items = temp;
}
class Program{
static void Main(string[] args){
ItemList items = new ItemList();
items.Add(new Item(1));
items.Add(new Item(2));
items.Add(new Item(3));
items.Add(new Item(4));
items.RemoveAt(2);
Console.WriteLine("ItemList count: {0}", items.Count());
Console.WriteLine("ItemList length: {0}", items.Size);
//ItemList count: 3
//ItemList length: 4
Console.Read();
}
}
using System;
namespace ArrayExample{
public class ItemList{
//===========CONSTANTS
private const int _defaultSize = 5;
//===========FIELDS
private Item[] _items;
//===========PROPERTIES
/// <summary>
/// Get the length of the collection
/// </summary>
public int Size {
get { return this._items.Length; }
}
/// <summary>
/// Select an item in the collection by its indexer
/// </summary>
/// <param name="index">The item index</param>
/// <returns>The item located at the specified index</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// If index is outside the boundries of the collection,
/// this exception will be thrown
/// </exception>
public Item this[int index] {
get {
if ((index < 0) || index >= this.Size) {
throw new ArgumentOutOfRangeException();
}
return this._items[index];
}
}
//===========CONSTRUCTOR
public ItemList(){
//Initate and set a default size for the array
this._items = new Item[_defaultSize];
}
//===========METHODS
/// <summary>
/// Adds an Item object to the collection
/// </summary>
/// <param name="item">The Item object to add</param>
public void Add(Item item) {
if (Count() == this.Size) {
//Create a new, temporary, array to hold the old values
Item[] tempArray = new Item[this.Size];
//Copy the old values to the temporary array
for (int i = 0; i < this.Size; i++) {
tempArray[ i] = this._items[ i];
}
//Create a new size by doubling the old size
int newSize = this.Size + this.Size;
//Create a new array with the new and improved size
this._items = new Item[newSize];
//Copy back the old items to the new and improved array
for (int i = 0; i < tempArray.Length; i++) {
this._items[ i] = tempArray[ i];
}
}
//Add the new item
int numberOfElements = Count();
this._items[numberOfElements] = item;
}
/// <summary>
/// Adds a range of Item objects to the collection
/// </summary>
/// <param name="items">An Item object array</param>
public void AddRange(Item[] items) {
foreach (Item i in items) {
this.Add(i);
}
}
/// <summary>
/// Returns the number of actual elements in the collection
/// </summary>
/// <returns>
/// An integer representing the actual elements in the collection
/// </returns>
public int Count() {
int elements = 0;
for (int i = 0; i < this.Size; i++) {
if (this._items[ i] != null) {
elements++;
}
}
return elements;
}
/// <summary>
/// Removes an element in the array.
/// Elements that comes after will move up one step
/// </summary>
/// <param name="index">The index of the element to remove</param>
public void RemoveAt(int index) {
//Create a temporary array
Item[] temp = new Item[this.Size - 1];
int count = 0;
//Loop through the array and copy the elements
for (int i = 0; i < this.Size; i++) {
//Don't copy if it is the element we want to remove
if ((i != index)) {
temp[count] = this._items[ i];
count++;
}
}
this._items = temp;
}
/// <summary>
/// Empties the collection
/// </summary>
public void Empty() {
this._items = new Item[_defaultSize];
}
}
}
using System;
namespace ArrayExample{
class Program{
static void Main(string[] args){
ItemList items = new ItemList();
items.Add(new Item(1));
items.Add(new Item(2));
items.Add(new Item(3));
items.Add(new Item(4));
items.RemoveAt(2);
Console.WriteLine("ItemList count: {0}", items.Count());
Console.WriteLine("ItemList length: {0}", items.Size);
//ItemList count: 3
//ItemList length: 4
Console.Read();
}
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.