using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
static void Main() {
var darr = new DynamicArray();
Console.WriteLine("length:" + darr.Count());
darr.Add(1);
Console.WriteLine("length:" + darr.Count());
darr.Add(2);
Console.WriteLine("length:" + darr.Count());
darr.Add(3);
Console.WriteLine("length:" + darr.Count());
darr.Add(44);
darr.Add(5);
darr.Add(6);
darr.Add(7);
Console.WriteLine("length:" + darr.Count());
Console.WriteLine("Index 4 of Value:" + darr.Get(4));
Console.WriteLine("Index 4 of Value:" + darr.Get(8));
/*Console.WriteLine("Index 4 of Value:" + darr.Get(9));*/
darr.WriteAllValues();
Console.WriteLine();
darr.Add(7);
darr.Add(7);
darr.Add(7);
Console.WriteLine("length:" + darr.Count());
darr.WriteAllValues();
}
}
public class DynamicArray
{
int capacity = 1;
int length = 0;
int[] arr = null;
public DynamicArray()
{
arr = new int[capacity];
}
public void Add(int n)
{
if (capacity == length)
{
this.Resize();
}
arr[length] = n;
length++;
}
public void Resize()
{
this.capacity *= 2;
int[] new_arr = new int[capacity];
for(int i = 0; i < arr.Length; i++)
{
new_arr[i] = arr[i];
}
arr = new_arr;
}
public int Get(int index)
{
if(index > arr.Length)
{
throw new Exception("Length is outbound");
}else
{
return arr[index - 1];
}
}
public int Count()
{
return arr.Length;
}
public void WriteAllValues()
{
foreach(int num in arr)
{
Console.Write(num + ",");
}
}
}