Results 1 to 7 of 7
  1. #1
    aqeel2010 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,537
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Writing generic array class

    Quote Originally Posted by JosAH View Post
    Arrays and generics don't go well together
    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
    where the class GenericClass can be instantiated with any class and then this instance is type safe for the used class in the instantiation.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,537
    Blog Entries
    7
    Rep Power
    20

    Default Re: Writing generic array class

    Quote Originally Posted by j2me64 View Post
    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
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    aqeel2010 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default Re: Writing generic array class

    The assignment is to implement a generic array class and generic linked linst class and liked list nod class.

  6. #6
    aqeel2010 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default Re: Writing generic array class

    Thanks a lot guys for this helpful discussion

  7. #7
    aqeel2010 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 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;
    		}
    	
    	
    	
    	
    	
    	
    }
    also, here is the main class for the experiments


    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

  1. Creating an array of generic type
    By colerelm in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 03:58 AM
  2. writing array values to another class
    By ronald christian in forum New To Java
    Replies: 27
    Last Post: 11-07-2008, 04:08 PM
  3. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 10:59 PM
  4. Writing generic methods
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:28 AM
  5. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •