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

    Default help with implementing Generic Array class

    I am trying to implement my generic array class but I have to many problems in this class


    please if someone have a look to the code and tell me what is wrong with my code , that will awesome, and I really appreciate it .



    Java Code:
    public class GenericArray<E >{
    	
    	private E size; 
    	private E  []items; 
    	
    	public GenericArray(){
    		
    		
    		
    	}
    	
    	public GenericArray GenericArray(){
    		
    		
    		GenericArray ga = new GenericArray();
    		
    		return ga;
    	}
    	
    	public E pushFront(E item){
    		
           E []newItems = new E [size+1]
    			
    		for (int i =0; i <size; ++i){
    			
    			newItems[i+1] = items[i];
    		}
    		
    		newItems[0] = item;
    		
    	}
    	
    	public E pushBack(E item){
    		
    		
    		E[] newItems = new E [size+1];
    		
    		for (int i = 0 ; i<size; ++i){
    			
    			newItems[i] = items [i];
    			
    		}
    		
    		newItems[size]= item; 
    	}
    	
    	public E popFront(){
    		int newItems[] = new int [size-1];
    		
    		for (int i =0; i<size-1; ++i ){
    			
    			
    			newItems[i] = items[i+1];
    			
    		}
    	}
    	
    	public E popBack(){
    		
    		
    		E newItems [] = new E [size-1];
    		
            for (int i = 0 ; i<size-1; ++i){
    			
    			newItems[i] = items [i];
    			
    		}
            
    	}
            public E insert(E item, E index){
            	
            	E newItems[] = new E [size+1];
            	
            	for(int i =0; i<index; ++i){
            		
            		newItems[i] = items[i];
            		
            	}
            	
            	 newItems[index]= item;
            	 
            	 for(int i = index; i<size; ++i ){
            		 
            		 newItems[i+1] = items[i]; 
            		 
            	 }
            	   	
            	
            	 
            }
            public E delete(E index){
            	
            E newItems [] = new E [size-1];
            	
            	for (int i = 0; i <index; ++i ){
            		
            		newItems[i] = items[i];
        
            		
            	}
            	
            	for (int i = index; i<size; ++i ){
            		
            		
            		
            		newItems[i-1] = items[i];
            	}
            	
            }
            
            public E getAt(E index){
            	
            	 E item = items[index];
            	 
            	 
            	 return item;
            	
            	
            }
            
            
            public E find(E item){
            	
            	
            	E index ;
            	for (E i = 0 ; i<size; ++i){
            		
            		
            		if (item == items[i]){
            			
            			
            		  index=i;
            		}
            		
            	}
            	  return index;
            	
            	
            }
            
    		
    		
    	
    	
    	
    	
    	
    	
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default Re: help with implementing Generic Array class

    have a look to the code and tell me what is wrong with my code
    It would be more straight forward if you told us what is wrong with the code.

    Specifically: does it compile? If not, and you cannot understand the compiler message(s) post the compiler output and say which lines of your code it is referring to.

    Does your program compile but have runtime behaviour which is not what you want/expect/intend? In that case post the actual code you are running and say what the runtime behaviour is (including exceptions) as well as what you wanted/expected/intended.

    -----

    This is a reasonably complicated task - that is, it is made up of a number of separate parts. It would be a very good idea if you dealt with each part, one at a time. Start with an empty class definition and add one constructor or method at a time. Deal with compiler messages as they arise. Completely test each constructor or method to verify that it does exactly as you want before moving on with the next. Know and be able to explain (perhaps even document!) what each method is supposed to do, and what the instance variables are intended to represent.

    I say all this both because I think it is the best way of making progress, but also because it provides you with a framework within which you can ask a precise question. The alternative a lump of code and a "doesn't work".

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

    Default Re: help with implementing Generic Array class

    I figured out what were the errors , and now I want to use the pushBack and pushFront methods for 200000 elements. I could not figure it out

    her is the code for the array class
    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;
    		}
    	
    	
    	
    	
    	
    	
    }
    here is the main class which I am trying to create an instance of the array class and pushBack and pushFront the elements. I could not figure it out.

    XML Code:
    import java.util.Arrays;
    
    
    public class ExpermintsDemo {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		
    		//GenericArray gaa = new GenericArray();
    	//	gaa.pushFront(0);
    		//gaa.pushFront(7);
    		//gaa.pushBack();
    		
    		/*
    		for (int i=0; i<gaa.getSize(); i++)
    		{
    			System.out.println(gaa.getAt(i));
    		}
    		*/
    		Integer[] a = RandomElements.randomArray(20, 100);
    		System.out.println(Arrays.toString(a));
    		
    		int i;
    		for ( i = 0; i<200000;i++){
    		GenericArray <Integer>strArr = new GenericArray<Integer>();
    		//strArr.pushFront(0);
    	   // strArr.insert(1, 1);
    		//strArr.insert(2, 2);
    		strArr.pushBack(9);
    		System.out.print(i);
    		
    		
    		}
    		System.out.print(i);
    		
    		
    		/*
    		System.out.println(strArr.popBack());
    		
    		for (int i=0; i<strArr.getSize(); i++)
    		{
    			System.out.print(strArr.getAt(i)+" ");
    		}
    		System.out.println("");
    		
    		System.out.println(strArr.popBack());
    		for (int i=0; i<strArr.getSize(); i++)
    		{
    			System.out.print(strArr.getAt(i)+" ");
    		}
    		//System.out.println(gaa.getSize());
    		*/
    		
    		
    		// TODO Auto-generated method stub
    
    	}
    
    }

    Please help

Similar Threads

  1. Writing generic array class
    By aqeel2010 in forum New To Java
    Replies: 6
    Last Post: 11-29-2011, 07:01 AM
  2. Creating an array of generic type
    By colerelm in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 04:58 AM
  3. Replies: 2
    Last Post: 12-14-2010, 05:10 PM
  4. A simple generic class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:41 PM
  5. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01: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
  •