C
C#4d ago
Tofaa

Array resizing issues

I'm so lost, i've been trying to resize my array Here's the code
internal class SingleThreadObjectArray<T> : IObjectArray<T>
{
private T[] array;
private int max;

internal SingleThreadObjectArray(int size)
{
array = new T[size];
max = size;
}

public T? Get(int index)
{
return index < array.Length ? array[index] : default(T);
}

public void Set(int index, T? value)
{
if (index >= array.Length)
{
var newLength = index * 2 + 1;
Array.Resize(ref array, newLength);
}
array[index] = value!;
max = Math.Max(max, index);
}

public void Trim()
{
array.CopyTo(array, max + 1);
}
}
internal class SingleThreadObjectArray<T> : IObjectArray<T>
{
private T[] array;
private int max;

internal SingleThreadObjectArray(int size)
{
array = new T[size];
max = size;
}

public T? Get(int index)
{
return index < array.Length ? array[index] : default(T);
}

public void Set(int index, T? value)
{
if (index >= array.Length)
{
var newLength = index * 2 + 1;
Array.Resize(ref array, newLength);
}
array[index] = value!;
max = Math.Max(max, index);
}

public void Trim()
{
array.CopyTo(array, max + 1);
}
}
However, whenever i try to set a higher index using Set, it throws an index out of bounds exception
18 Replies
Tofaa
Tofaa4d ago
I tried using Array.CopyTo and then value = the array given by CopyTo, but nothing works
Angius
Angius4d ago
Use the debugger to see what the values are after resizing?
Angius
Angius4d ago
Resizing seems to work fine on my end
No description
SpReeD
SpReeD4d ago
Stupid question, but, why not use List instead?
Angius
Angius4d ago
You can't set list items at arbitrary indexes, the above is probably supposed to allow setting the 8376th element of a 3-element array I guess depending how large the gaps are supposed to be, though, I would maybe consider a dictionary though
Tofaa
Tofaa4d ago
yup theyre not that drastic but i've found out this is much more performance efficient in my scenario atleast in my java iteration of this project, havent tested the c# one but im guessing it'd be similar
SpReeD
SpReeD4d ago
Or giving the member an Id property to access the object directly?
Tofaa
Tofaa4d ago
im far less familiar with the toolchain in the language to be able to do this This is used as a registry
Angius
Angius4d ago
It's nothing complex. Set a breakpoint, run the debug, watch the values $debug if you want a full tutorial
MODiX
MODiX4d ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Tofaa
Tofaa4d ago
I dont particularly know what id will be set up until the startup of the project Thank you very much
Angius
Angius4d ago
So it is possible that you'll end up with large gaps? If so, then I would really just consider a dictionary
Tofaa
Tofaa4d ago
welp the debuggger has spewed a LOT of stuff i do not understand
Angius
Angius4d ago
Like what?
Tofaa
Tofaa4d ago
Kinda everything
No description
Tofaa
Tofaa4d ago
it's intimidating at first Might just end up doing this
Angius
Angius4d ago
Seeing how the name of this class says "single thread", I assume some threading is involved somewhere. There's also a ConcurrentDictionary that's thread-safe, should you need it
Tofaa
Tofaa4d ago
yup there's one thats volatile and locked and one that's just plain old array without anything to synchroizeit