Writing generic array class
Hello my friends, I have assignment to he write generic array class include the following methods:
. no argument constructor
. copy constructor
. PushFront()
.PushBack()
. PopFront()
PopBack()
. Insert()
. Delete()
.GetAt()
.find()
and I have no Idea how to start. Please any suggestion to start, or any material that will help me to understand the assignment.
Thanks:frusty::frusty:
Re: Writing generic array class
Arrays and generics don't go well together; better encapsulate a List<T> in your class because it has most (all?) of those methods available; all your class has to do then is delegate to those methods.
kind regards,
Jos
Re: Writing generic array class
Quote:
Originally Posted by
JosAH
Arrays and generics don't go well together
Right, but the ot means perhaps something like
Code:
public class GenericClass <T> {
List<T> list = new ArrayList<T>();
public void add(T t) {
list.add(t);
}
// other methods
where the class GenericClass can be instantiated with any class and then this instance is type safe for the used class in the instantiation.
Re: Writing generic array class
Quote:
Originally Posted by
j2me64
Right, but the ot means perhaps something like
Code:
public class GenericClass <T> {
List<T> list = new ArrayList<T>();
public void add(T t) {
list.add(t);
}
// other methods
where the class GenericClass can be instantiated with any class and then this instance is type safe for the used class in the instantiation.
Yep, that's what I also suggested in my reply.
kind regards,
Jos
Re: Writing generic array class
The assignment is to implement a generic array class and generic linked linst class and liked list nod class.
Re: Writing generic array class
Thanks a lot guys for this helpful discussion
Re: Writing generic array class
I have writtin the class , and I hope it works as it should. The problem know is that how to push 200000 elements to the array. I have tried small numbers. Know I need help in how to using pushFront() for these 200000 elements. Code:
public class GenericArray<E >{
//private int size;
private E[] items;
public GenericArray(){
this.items = (E[])new Object[0];
}
public GenericArray(E []ga){
this.items = (E[])new Object[ga.length];
for (int i=0; i<ga.length;i++){
this.items[i]=ga[i];
}
}
public void pushFront(E item){
E[] newItems = (E[])new Object[items.length+1];
for(int i=1; i<newItems.length; i++){
newItems[i] = items[i-1];
}
newItems[0]=item;
this.items = newItems;
}
public void pushBack(E item){
E[] newItems = (E[])new Object[items.length+1];
for (int i=0; i<items.length; i++){
newItems[i] = items[i];
}
newItems[newItems.length-1] = item;
this.items = newItems;
}
public E popFront(){
E item=items[0];
this.delete(0);
return item;
}
public E popBack(){
E item = items[items.length-1];
this.delete(items.length-1);
return item;
}
public void insert(E item, int index){
E[] newItems = (E[]) new Object[items.length+1];
//E newItems[] = new E [size+1];
for(int i =0; i<index; i++){
newItems[i] = items[i];
}
newItems[index]= item;
for(int i = index+1; i<items.length+1; i++ ){
newItems[i] = items[i-1];
}
this.items = newItems;
}
public void delete(int index){
E[] newItems = (E[]) new Object[items.length-1];
//E newItems [] = new E [size-1];
for (int i = 0; i <index; i++ ){
newItems[i] = items[i];
}
for (int i = index+1; i<items.length; i++ ){
newItems[i-1] = items[i];
}
this.items = newItems;
}
public E getAt(int index){
E item = items[index];
return item;
}
public int find(E item){
int index ;
for (int i = 0 ; i<items.length; i++){
if (item == items[i]){
index=i;
return index;
}
}
return -1;
}
public int getSize(){
return this.items.length;
}
}
also, here is the main class for the experiments
Code:
public class ExpermintsDemo {
/**
* @param args
*/
public static void main(String[] args) {
GenericArray <Integer>gaa = new GenericArray<Integer>();
//GenericArray gaa = new GenericArray();
gaa.pushFront(0);
//gaa.pushFront();
gaa.pushBack(7);
gaa.popFront();
gaa.insert(1,1);
for (int i=0; i<gaa.getSize(); i++)
{
System.out.print(gaa.getAt(i));
}
//GenericArray <Integer>strArr = new GenericArray<Integer>();
//int i;
//for ( i = 0; i<2;i++){
// strArr.insert(1, 1);
//strArr.insert(2, 2);
// strArr.pushFront(0);
// strArr.popFront();
// strArr.popFront();
//System.out.println(strArr.pushFront());
// System.out.println(i);
}
//System.out.println(strArr.popBack());
// TODO Auto-generated method stub
}
please guys any help wit figuring out this project
I am new to java