2

I am having trouble understanding how to pass a linkdlist into a method that display that list as a stack. I am aware i havent specifed the type of list but my instructor said that it would not matter for this purpose. but I am still learning so I'm not to sure if I am passing the linkedlist correctly into the method.

import java.util.LinkedList ;
import java.util.ListIterator;

public class UseStacksAndQueues{

  public static void main(String[] args) {

      StacksAndQueues sQ = new StacksAndQueues();
      String [] days = {"mon","tue","wed","thur","fri", "sat","sun"};
      LinkedList aList = new LinkedList();
      LinkedList newList = new LinkedList();

      //load array of string objects into linked list
      aList = sQ.methodOne(days);

      //display linked list as a stack
      sQ.methodTwo(aLits);

the method.

//display a linked list as a stack 
public LinkedList methodTwo(aList){

     for(int i = aList.size; i <= 0; i--)
     {
        System.out.println(aList.get(i));

     }

}//end method two
S.Q
  • 155
  • 2
  • 15

2 Answers2

1

that would be correct. in java, objects are passed by reference (except for primitive types) so you will be able to perform all of the operations on the list you pass to methodTwo.

Please refer to this post for an explanation

Is Java "pass-by-reference" or "pass-by-value"?

Having said that, you do not have any types associated with your List objects, so you will need to specify that.

so something like

public void methodTwo(LinkedList<String> aList){

 for(int i = aList.size; i <= 0; i--)
 {
    System.out.println(aList.get(i));

 }

 }//end 

i have the return type as void, as that is all thats needed if you only need methodTwo for display purpose

and you will also need to declare as

LinkedList<String> aList = new ArrayList<String>();
Community
  • 1
  • 1
AbtPst
  • 7,778
  • 17
  • 91
  • 172
  • 2
    "in java, objects are passed by reference" is not accurate – dragon66 Oct 27 '15 at 18:23
  • except for primitive types, aren't all other objects passed by reference? – AbtPst Oct 27 '15 at 18:24
  • 1
    No, I am talking about object too. See the related post http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 – dragon66 Oct 27 '15 at 18:25
  • 2
    @AbtPst Perhaps a better way to phrase it is "Java is 'pass reference by value'". – Mage Xy Oct 27 '15 at 18:27
  • correct ! i just wanted to show that it is easy to manipulate objects in Java. i will include the above link in my answer. thanks – AbtPst Oct 27 '15 at 18:28
  • 1
    LinkedList is not an interface. List is. – dragon66 Oct 27 '15 at 18:30
  • so then is ArrayList a subclass of LinkedList? – AbtPst Oct 27 '15 at 18:30
  • 1
    [`LinkedList`](http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#LinkedList()) inherits from `AbstractSequentialList` inherits from `AbstractList`. [`ArrayList`](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) inherits directly from `AbstractList`. So `ArrayList` is like `LinkedList`'s uncle. – Rainbolt Oct 27 '15 at 18:32
1

Your call to the method is correct. The method itself is the problem. You need to specify the type of object being passed into your method.

public LinkedList<String> methodTwo(LinkedList<String> aList){
    ...
}

You also need to specify the type of your LinkedList in angled brackets, as shown above. That includes when you create your list before passing it around.

LinkedList<String> aList = new LinkedList<>();

The second pair of angled brackets can be empty, as shown above. This is a shortcut introduced in Java 7.

Rainbolt
  • 3,542
  • 1
  • 20
  • 44