Jump to content
  • Advertisement
Sign in to follow this  
Sfpiano

[java] Beginning question involving arrayList

This topic is 4772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

main() {
class Edge {
			private int s, e, w;
			public Edge(int st, int en, int wt) {
				s = st;
				e = en;
				w = wt;
			}
			public int GetS() {
				return s;
			}
			public int GetE() {
				return e;
			}
			public int GetW() {
				return w;
			}
		}
		
		class Graph {
			private Set<Integer> nodes = new TreeSet<Integer>();
			private List<Edge> edges = new ArrayList<Edge>();
			
			public void AddNode(int i) {
				nodes.add(i);
			}
			
			public void AddEdge( Edge e ) {
				edges.add( e );
			}
			
			public void Dijkstra() {
				int i;
				Set<Integer> pathSet = new TreeSet<Integer>();
				List<Integer> wtList = new ArrayList<Integer>();
				for(i=0; i<5; i++) {
					wtList.add(-1);
				}
				int cur, next;
				cur = 1;
				
				while( pathSet.size() < 5 ) {
					for(i=0; i<edges.size(); i++) {
						if( edges.GetS() == cur || edges.GetE() == cur ) {
							wtList = edges.GetW();
						}
					}
				}
			}
		}
}
On the line wtList = edges.GetW() and the one above, the compiler tells me: The type of the expression must be an array type but it resolved to List<Integer>

Share this post


Link to post
Share on other sites
Advertisement
The problem is you're treating an ArrayList like an array. ArrayLists don't use the list syntax. Try list.get(i).

Share this post


Link to post
Share on other sites
Java does not provide operator overloading. For Set and List classes, you will need to use the appropriate method (IIRC it is ".at(subscript)" in both cases) to access the element.

Share this post


Link to post
Share on other sites
I think the List interface uses get(i) returning an Object in Java 1.4, so in 1.5 it should return an Edge. In which case you should be able to chain the call as in: edges.get(i).GetS(), but in order to add to the list you'll have to do something like:

// where the parameters are the index, object
wtList.add(i, edges.get(i).GetS() );

Although you have probably figured this out by now -- however if you are planning on over writing the weight or wtList sub i, you'll have to remove the entry and then add it in at the desired index, unfortunetly IIRC there is no "swap" at the index.

L-

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!