Results 1 to 7 of 7
Thread: Writing generic array class
- 11-22-2011, 11:29 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
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
- 11-22-2011, 11:55 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-22-2011, 12:20 PM #3
Re: Writing generic array class
Right, but the ot means perhaps something like
Java Code:public class GenericClass <T> { List<T> list = new ArrayList<T>(); public void add(T t) { list.add(t); } // other methods
- 11-22-2011, 12:24 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 11-22-2011, 12:25 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Re: Writing generic array class
The assignment is to implement a generic array class and generic linked linst class and liked list nod class.
- 11-22-2011, 12:26 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Re: Writing generic array class
Thanks a lot guys for this helpful discussion
- 11-29-2011, 07:01 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
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.
XML 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; } }
XML 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
Similar Threads
-
Creating an array of generic type
By colerelm in forum New To JavaReplies: 1Last Post: 10-19-2011, 04:58 AM -
writing array values to another class
By ronald christian in forum New To JavaReplies: 27Last Post: 11-07-2008, 05:08 PM -
writing an array class
By wardd85 in forum New To JavaReplies: 5Last Post: 07-16-2008, 11:59 PM -
Writing generic methods
By eva in forum New To JavaReplies: 2Last Post: 12-31-2007, 04:28 AM -
Generic array
By eva in forum New To JavaReplies: 3Last Post: 12-23-2007, 01:12 AM
Bookmarks