Post: List [Class][C++]
03-16-2011, 11:12 PM #1
kiwimoosical
Bounty hunter
(adsbygoogle = window.adsbygoogle || []).push({});
    template<typename type>
class List
{
private:
type list[];
public:
type get(int);
void add(type);
void remove(int);
int size();
bool isEmpty();
bool contains(type);
void trim();
List();
~List();
};

template<typename type>
List<type>::List()
{
list = new type[1];
}

template<typename type>
List<type>::~List()
{
list = NULL;
}

template<typename type>
type List<type>::get(int index)
{
return list[index];
}

template<typename type>
void List<type>::add(type obj)
{
type temp[] = new type[list.length + 1];
for(int ti = 0; ti < size(); ti++)
temp[ti] = (Object)list[ti];
temp[list.length] = element;
}

template<typename type>
void List<type>::remove(int index)
{
list[index] = NULL;
}

template<typename type>
int List<type>::size()
{
return (sizeof(list) / sizeof(list[0])) - 1 > 0 ?
sizeof(list) / sizeof(list[0]) :
0;
}

template<typename type>
bool List<type>::isEmpty()
{
return size() > 0;
}

template<typename type>
bool List<type>::contains(type obj)
{
for(int i = 0; i < list.length; i++)
if(list[i]== obj)
return true;
return false;
}

template<typename type>
void List<type>::trim()
{
int count = 0;

for(int i = 0; i < size(); i++)
{
if(!list[i] == NULL)
count++;
}

if(count > 0)
{
type temp[] = new type[count];
count = 0;

for(int i = 0; i < count; i++)
{
if(!list[i] == NULL)
{
temp[count] = list[i];
count++;
}
}
}
}


This is my version of the Java ArrayList class Smile
Enjoy.

The following 2 users say thank you to kiwimoosical for this useful post:

Ritztro, TheUberFail
03-17-2011, 07:21 PM #2
kiwimoosical
Bounty hunter
Why do I even bother posting, no one understands my work, it's pointless.
03-17-2011, 10:32 PM #3
Ritztro
I am a Game Developer
Originally posted by kiwimoosical View Post
Why do I even bother posting, no one understands my work, it's pointless.


Now don't say that! I just got to it lol. It looks very nice. Except I am a little lost on the add function as idk where element and Object came from.. Also I don't really follow what the function does does. I think I understand what the whole list idea is though. You just create a list of arrays correct? Could you please explain this too me as I would like to fully understand and not just partly.
03-17-2011, 10:58 PM #4
kiwimoosical
Bounty hunter
Originally posted by Dutch View Post
Now don't say that! I just got to it lol. It looks very nice. Except I am a little lost on the add function as idk where element and Object came from.. Also I don't really follow what the function does does. I think I understand what the whole list idea is though. You just create a list of arrays correct? Could you please explain this too me as I would like to fully understand and not just partly.


It's more so of an array handling class, it creates a one dimensional array of <typename type> and then gives you more methods than the regular array class.

Copyright © 2025, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo