title: Get method in a implemented Linked List, Java
body: <p>I need to implement a Linked List of integers from zero (not using existing LinkedList class).</p>
  
  <p>This is the code:</p>
  
  <p><em>A single Link class</em></p>
  
  <pre><code>public class Link {
      public int data;
      public Link nextLink;
  
      public Link(int d1) {
          data = d1;      
      }
  
      public void printListElements(){
          System.out.println(data);
      }
  }
  </code></pre>
  
  <p><em>and the LinkedList class</em></p>
  
  <pre><code>public class LinkedList {
      private Link first;
      public LinkedList(){
          first = null;
      }
  
      public void add(int data1){
          Link linklist = new Link(data1);
          linklist.nextLink = first;
          first = linklist;
      }
  
      public void printList(){
      Link current=first;
      System.out.println("List Elements are ");
      while(current!=null){
         current.printListElements();
         current=current.nextLink;
      }
    }
  }
  </code></pre>
  
  <p>As you see it has already <em>add</em> and <em>printList</em> method. But how do i make a get() method, which returns a value of a specific index.</p>
  
  <p>This is what i mean:</p>
  
  <pre><code>public static void main(String args[]){
      LinkedList MyList = new LinkedList();
  
      MyList.add(1);
      MyList.add(2);
      MyList.add(3);
      MyList.add(4);
  
      System.out.println("MyList.get(0)"); // should get 1
          System.out.println("MyList.get(1)"); // should get 2 etc
  }
  </code></pre>
  
  <p>Thank You in advance.</p>