Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help creating arraylist of stacks

 
John Laker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm implementing Dijkstra's algorithm in Java. I need help in creating an arraylist of stacks that will be used to store the predecessors table, which means that for every element's shortest path, the predecessors taken to get there would be in the stack.

Here's the code I have


I'm unable to add the the stack to prev arraylist. The error is

The method add(E) in the type ArrayList<E> is not applicable for the arguments (predec<E>)
.

I have implemented a TreeMap, with the mainNodes arraylist storing all the elements in the map.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/java/generics/index.html
http://download.oracle.com/javase/tutorial/extra/generics/index.html
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably meant ArrayList<Stack<WhateverClassTheStackHolds>>.
 
John Laker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You probably meant ArrayList<Stack<WhateverClassTheStackHolds>>.


oh, so you mean define arraylist like

ArrayList <Stack <E>> since stack needs to hold generic type objects of type E ?
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You probably meant ArrayList<Stack<WhateverClassTheStackHolds>>.

Thanks Rob, that's right. I was careless.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Laker wrote:
Rob Prime wrote:You probably meant ArrayList<Stack<WhateverClassTheStackHolds>>.


oh, so you mean define arraylist like

ArrayList <Stack <E>> since stack needs to hold generic type objects of type E ?

Only if you want to use a Stack that holds objects of class E (that is, if you really have a class named E). So, no, not likely.

What class of objects is the Stack meant to hold?
 
Rob Spoor
Sheriff
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if E is either a class, or a generic type of the class / method this code runs in, then it would be good.
 
John Laker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Well, if E is either a class, or a generic type of the class / method this code runs in, then it would be good.


yes, E is a generic type object
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic