Search code examples
javaarraysdata-structureslinked-listadt

Linked List Array Based Implementation- Print Method Not Compiling


For an Assignment we had previously implemented An Array Based Linked List using a node class however, she has requested all students redo this assignment without using the Node class. I have everything working as intended except for my printList Method- Can you please review my printList Method and let me know your thoughts? Below is the class and the driver.

Linked List Class

public class LinkedListArray {

   int size;
   int maxSize;
   int arrayVal[];
   
   public LinkedListArray()
   {
       this.size = 10;
   }
  
   public LinkedListArray(int list)
   {
       if(list < 0)
           maxSize = 20;
       else
           maxSize = list;
       
       size = 0;
       arrayVal = new int[maxSize];
   }
      

   public int size(){
       return size;
   }
   
   public boolean isEmpty() {   
       return (size() == 0);
   }
   
   public boolean isFull(){
       return (size() == maxSize);
   }
  

   public void addHead(int value){
   
       if(! this.isFull()){
           for(int i = size; i > 0; i--)
               arrayVal[i] = arrayVal[i - 1];
           
           arrayVal[0] = value;
           size++;
          
       }else
           System.out.println("The List is Full.");
   }
      

   public void addTail(int value){
       
       if(! this.isFull()){
           arrayVal[size] = value;
           size++;
          
       }else
           System.out.println("The List is Full.");
   }
   
   public int removeHead() {
      
       if(! this.isEmpty())
       {
           int value = arrayVal[0];


           for(int i = 0; i < size -1; i++){
               arrayVal[i] = arrayVal[i+1];
           }
           
           size--;
           return value;
          
       }else 
           System.out.println("The List is Empty.");
       
       return arrayVal[size];
   }


   public int removeTail(){
       
       if(!isEmpty()){ 
           size--; 
          
       }else 
           System.out.println("The List is Empty.");
       
       return arrayVal[size];
   }

  

   public String printList(){
       
       String listString = new String();
       
       if(! this.isEmpty())
           return "The List is Empty.";
   
       for(int i = 0; i < size; i++) {
           listString += arrayVal[i];
               
           if(i < size -1) {
               listString += ", ";
           }
       }  
       return "Elements in the List: " + listString;
   } 
   }

Driver Class

  public class ProgramDriver {

  public static void main(String[] args) {

       LinkedListArray lla = new LinkedListArray(10);
       
       System.out.println("Size of List: " + lla.size());
       System.out.println("Is the list empty?: " + lla.isEmpty());
       System.out.println("Is the list full?: " + lla.isFull());
       
       lla.addHead(4);
       lla.addHead(5);
       lla.addTail(10);
       lla.addTail(9);
       lla.addTail(7);
       lla.addTail(2);
       lla.addHead(8);
       lla.addHead(1);
       lla.addTail(3);
       lla.addTail(6);
       
       lla.printList();
          
       System.out.println("Size of List: " + lla.size());
       System.out.println("Is the list empty?: " + lla.isEmpty());
       System.out.println("Is the list full?: " + lla.isFull());
       
      lla.removeHead();
      lla.removeTail();
      
      System.out.println("Size of List: " + lla.size());
      System.out.println("Is the list empty?: " + lla.isEmpty());
      System.out.println("Is the list full?: " + lla.isFull());
      
      lla.printList();  
  }
 }

Solution

  • First: StackOverflow isn't a code review site. Don't just post code and ask people what they think. (Look into https://codereview.stackexchange.com/ for that)

    So when asking a question like this, it's very helpful to specify what behavior you're seeing, and in what way it's not what you expect. That way we can help you solve the specific problem you're actually running into.

    That said, this is obviously a problem:

           if(! this.isEmpty())
               return "The List is Empty.";
    

    If the list is not empty, why would you say "The List is Empty."? I'm guessing you want to remove the ! there.

    Apart from that, string concatenation in a loop can get expensive if you end up with a lot of items in your list. You should avoid it. Look into String.join() or StringBuilder.