Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Collection_ basic datastructure brushup
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class DynamicArray { private object[] _objectArray=null; private int _nextOpenElement=0; private int _growthIncrement=10; private const int INITIAL_SIZE=25; public int Count { get{return _nextOpenElement;} } public object this[int index] { get { if((index >=0) && (index <= _objectArray.Length)) return _objectArray[index]; else throw new ArgumentOutOfRangeException(); } set { if( _nextOpenElement < _objectArray.Length) _objectArray[_nextOpenElement++]=value; else { GrowArray(); _objectArray[_nextOpenElement++]=value; } } } public DynamicArray(int size) { _objectArray=new object[size]; } public DynamicArray():this(INITIAL_SIZE){} public void Add(object o) { if( _nextOpenElement < _objectArray.Length) _objectArray[_nextOpenElement++]=o; else { GrowArray(); _objectArray[_nextOpenElement++]=o; } } private void GrowArray() { object[] _tempArray= _objectArray; _objectArray=new object[_objectArray.Length + _growthIncrement]; for(int i=0,j=0;i < _tempArray.Length;i++) { if( _tempArray[i] != null) { _objectArray[j++] = _tempArray[i]; } _nextOpenElement=j; } _tempArray=null; } } public class Program { public static void Main(string[] args) { DynamicArray da=new DynamicArray(); Console.WriteLine("The Array contains "+ da.Count+" of objects"); da.Add("Oh I Love C# Like Anything"); Console.WriteLine(da[0].ToString()); for(int i=1;i<56;i++) { da.Add(i); } Console.WriteLine("Now The Array contains "+ da.Count+" of objects"); da.Add("Oh I Love C# Like Anything Even Now"); //Console.WriteLine(da[0].ToString()); for(int i=1;i<da.Count;i++) { if(da[i]!=null) { Console.Write(da[i].ToString()+ " , "); if((i%10)==0) { Console.WriteLine(); } } } } } }
run
|
edit
|
history
|
help
0
Hallo world
axasxasd
Const
form
BaseUrl test
eze
Generics Operator test
Olymp
|= Caching
Search Element In Array-Recursive