C
C#4w ago
Faker

Feedback on small code exercises

C#
class CustomList<T>
{
private T[] items;
private int size;

public CustomList()
{
items = new T[10];
size = 0;
}

// Add method
public void Add(T item)
{
// Check if resizing is needed
if (size >= items.Length)
{
// Array.Resize(ref items, size * 2);
Resize();
}

// Add item
items[size] = item;
size++;
}

public void PrintArray()
{
foreach (var item in items)
{
Console.Write(item + " ");
}
}


// Remove method
public void Remove(T item)
{
var indexResult = Array.IndexOf(items, item);
if (indexResult != -1)
{
// item exist - should be removed
for (int i = indexResult; i <= size; i++)
{
// Shift all elements after the found index to the left
(items[indexResult], items[i]) = (items[i], items[indexResult]);
}

// Set the last element to default(T) and decrease the size
items[size] = default(T);
size--;
}
else
{
Console.WriteLine("item doesn't exist");
}

}

private void Resize()
{
// Double the capacity
var newCapacity = items.Length * 2;
// Create a new larger array
var newArray = new T[newCapacity];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = default(T);
}
// Copy old elements into the new array
var counter = 0;
foreach (var item in items)
{
newArray[counter] = item;
counter++;
}
// Reassign the items array to the new array
items = newArray;
}

}
C#
class CustomList<T>
{
private T[] items;
private int size;

public CustomList()
{
items = new T[10];
size = 0;
}

// Add method
public void Add(T item)
{
// Check if resizing is needed
if (size >= items.Length)
{
// Array.Resize(ref items, size * 2);
Resize();
}

// Add item
items[size] = item;
size++;
}

public void PrintArray()
{
foreach (var item in items)
{
Console.Write(item + " ");
}
}


// Remove method
public void Remove(T item)
{
var indexResult = Array.IndexOf(items, item);
if (indexResult != -1)
{
// item exist - should be removed
for (int i = indexResult; i <= size; i++)
{
// Shift all elements after the found index to the left
(items[indexResult], items[i]) = (items[i], items[indexResult]);
}

// Set the last element to default(T) and decrease the size
items[size] = default(T);
size--;
}
else
{
Console.WriteLine("item doesn't exist");
}

}

private void Resize()
{
// Double the capacity
var newCapacity = items.Length * 2;
// Create a new larger array
var newArray = new T[newCapacity];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = default(T);
}
// Copy old elements into the new array
var counter = 0;
foreach (var item in items)
{
newArray[counter] = item;
counter++;
}
// Reassign the items array to the new array
items = newArray;
}

}
5 Replies
Faker
FakerOP4w ago
C#
var list = new CustomList<int>();
list.Add(5);
list.Add(10);
list.Add(100);
list.PrintArray();
Console.WriteLine();
C#
var list = new CustomList<int>();
list.Add(5);
list.Add(10);
list.Add(100);
list.PrintArray();
Console.WriteLine();
Hello guys, can someone give some feedbacks about the way I code to implement what the exercises were asking for please, like adding an item/removing an item etc I could have use a list but it's a class exercise so we must follow the template What could I improve please, also, when I use the default(T), I get the warning that T might be null, so I should always assign the default value to a variable, check if the variable is not null, then assigned to the array in this case?
Sossenbinder
Sossenbinder4w ago
Sounds like a #code-review thing What was the exercise though? Reimplementing List<T>?
Faker
FakerOP4w ago
ops sorry, will post that there euh wait I don't have the template but basically, I needed to implement the add method/remove method/ resize method without using any in-built array method
Sossenbinder
Sossenbinder4w ago
Anyways, some general remarks: - If Remove fails, you'd want to throw an exception or at least return that status somehow. Writing to Console won't help the control flow of your app usually. - It's not necessary to initialize the new array in your Resize if you end up copying things over next Thought so yeah
Faker
FakerOP4w ago
ahh I see 2sec, will just repost the post in code re-view I will just close this post, will keep in mind what you said about the remove and resize method

Did you find this page helpful?