Last active
February 2, 2016 21:32
-
-
Save DeepSky8/1d3163b8a8f59d6cbfb5 to your computer and use it in GitHub Desktop.
Each of the methods appear to work as intended.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ArrayList | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//var newList = new ArrayBackedList<int>(12); | |
//newList.Add(1); | |
//newList.Add(2); | |
//newList.Add(3); | |
//newList.Add(4); | |
//newList.Add(5); | |
//newList.Add(6); | |
//newList.Add(7); | |
//newList.Add(8); | |
//newList.Add(9); | |
//newList.Add(10); | |
//newList.Add(11); | |
//newList.Add(12); | |
//Console.WriteLine(newList.ToString()); | |
//Console.WriteLine("\nThis list is {0} items long.", newList.Count().ToString()); | |
//Console.WriteLine("\n\nWhat element would you like to add to the list? The list is currently of type {0}", newList.Get(0).GetType().ToString()); | |
//var addElement = Console.ReadLine(); | |
//newList.Add(Convert.ToInt32(addElement)); | |
//Console.WriteLine("\n\n\nThe updated list can hold up to {0} elements. It currently consists of: {1}", newList.TotalSize().ToString(), newList.ToString()); | |
//Console.WriteLine("\nThe list currently holds {0} elements.", newList.Count().ToString()); | |
//Console.WriteLine("\n\nWhat element would you like to place in the list, replacing some other element? The list is currently of type {0}", newList.Get(0).GetType().ToString()); | |
//var replaceElement = Console.ReadLine(); | |
//Console.WriteLine("\nAnd in which location in the list would you like place {0}?", replaceElement); | |
//var replaceLocation = Console.ReadLine(); | |
//newList.Replace(Convert.ToInt32(replaceElement), (Convert.ToInt32(replaceLocation)-1)); | |
//Console.WriteLine("\n\n\nThe updated list can hold up to {0} elements, and currently consists of: {1}", newList.TotalSize().ToString(), newList.ToString()); | |
//Console.WriteLine("\n\nWhat would you like to insert in the list? The list is currently of type {0}", newList.Get(0).GetType().ToString()); | |
//var insertElement = Console.ReadLine(); | |
//Console.WriteLine("\nAnd in which location in the list would you like place {0}?", insertElement); | |
//var insertLocation = Console.ReadLine(); | |
//newList.Replace(Convert.ToInt32(insertElement), (Convert.ToInt32(insertLocation)-1)); | |
//Console.WriteLine("\n\n\nDoes this list contain elements? {0}", newList.Any().ToString().ToUpper()); | |
//Console.WriteLine("\n\n\nThe updated list can hold up to {0} elements, and currently consists of: {1}", newList.Count().ToString(), newList.ToString()); | |
//var secondList = new ArrayBackedList<string>(); | |
//Console.WriteLine("\n\n\nDoes the new list contain elements? {0}", secondList.Any().ToString().ToUpper()); | |
//Console.WriteLine("How many elements can the second list contain? {0}", secondList.TotalSize().ToString()); | |
} | |
public class ArrayBackedList<T> | |
{ | |
int usedSlots = 0; | |
int arraySize = 0; | |
T[] abl = new T[0]; | |
/// <summary> | |
/// Provide an integer size for the interal array to initialize. | |
/// </summary> | |
/// <param name="size"></param> | |
public ArrayBackedList(int size) | |
{ | |
arraySize = size; | |
abl = new T[arraySize]; | |
} | |
/// <summary> | |
/// Default 128-element array initialized | |
/// </summary> | |
/// <param name="size"></param> | |
public ArrayBackedList() | |
{ | |
arraySize = 128; | |
abl = new T[arraySize]; | |
} | |
/// <summary> | |
/// Provide an element of type T (set when the list was created), which is inserted at the end of the used portion of the array. If the array has reached its maximum size, the array size is doubled, and then the element is added. | |
/// </summary> | |
/// <param name="element"></param> | |
public void Add(T element) | |
{ | |
//If the current array is full, need to create a new array that is double the size, copy all the current values at their current index locations to the new list, and then replace the former array with the new one | |
if (usedSlots == arraySize) | |
{ | |
var biggerArray = arraySize * 2; | |
var arrayUpdate = new T[biggerArray]; | |
for (int i = 0; i < arraySize; i++) | |
{ | |
arrayUpdate[i] = abl[i]; | |
} | |
arraySize = biggerArray; | |
abl = arrayUpdate; | |
} | |
abl[usedSlots] = element; | |
usedSlots += 1; | |
} | |
/// <summary> | |
/// Creates a new array, with twice the space if necessary, then copies all values up to the insert location. Inserts new element, then copies the rest of the array to the following array locations | |
/// </summary> | |
/// <param name="element"></param> | |
/// <param name="index"></param> | |
public void Insert(T element, int index) | |
{ | |
var biggerArray = arraySize * 2; | |
var arrayUpdate = new T[arraySize]; | |
//If the updated array would fall outside the current array size, double the array size | |
if (usedSlots == arraySize) | |
{ | |
arrayUpdate = new T[biggerArray]; | |
} | |
// Copy all elements up to the insert location to the new array | |
for (int i = 0; i < index; i++) | |
{ | |
arrayUpdate[i] = abl[i]; | |
} | |
//Add element at insert location | |
arrayUpdate[index] = element; | |
//Copy the rest of the array to the following array locations | |
for (int i = (index+1); i < (arraySize+1); i++) | |
{ | |
arrayUpdate[i] = abl[i - 1]; | |
} | |
//Update the size field for accurate processing in the future | |
arraySize = biggerArray; | |
//Replace the old array with the updated array | |
abl = arrayUpdate; | |
// | |
usedSlots += 1; | |
} | |
public void Replace(T element, int index) { abl[index] = element; } | |
public T Get(int index) { return abl[index]; } | |
public int Count() { return usedSlots; } | |
public int TotalSize() { return arraySize; } | |
public bool Any() { return !(usedSlots == 0); } | |
public override string ToString() | |
{ | |
var items = new string[Count()]; | |
for (int i = 0; i < items.Length; i++) | |
{ | |
items[i] = Get(i).ToString(); | |
} | |
return string.Format("{0}", string.Join(", ", items)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment