The Artima Developer Community
Sponsored Link

Java Answers Forum
returning nothing

2 replies on 1 page. Most recent reply: May 10, 2002 7:12 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
neil

Posts: 5
Nickname: neil
Registered: May, 2002

returning nothing Posted: May 10, 2002 5:31 PM
Reply to this message Reply
Advertisement
Does anyone know why this does not return position?
public int newJobs()
{
QueueNode temp = new QueueNode();

int position = 0;
temp = start;
while(temp!= null)
{
temp = temp.previous;
position = position + 1;
}
return position;
}


It works when I put void in it though...
public void newJobs()
{
QueueNode temp = new QueueNode();

int position = 0;
temp = start;
while(temp!= null)
{
temp = temp.previous;
position = position + 1;
}
System.out.println(position);
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: returning nothing Posted: May 10, 2002 7:05 PM
Reply to this message Reply
What do you mean it doesn't return the position? What does it return? What are you trying to accomplish? If it is supposed to get the position why isn't the method called getPosition() instead of newJobs(), which would lead us to believe that it instantiates an array of Job objects, or something?

More specifically, you assigned temp to a new instance of QueueNode, but then before using it, you replaced it with an assignment to start (which I assume is an instance variable). Then, from start, you traverse to previous, which seems counterintuitive (I would imagine going to the next node from the start node, or the previous node from the end, or last node). Did you also write the QueueNode class?

Anyway, here is a little more concise version of your code, keeping to the same conventions you have (that is, going from start to previous, rather than next):
public int getNodeCount()
{
	int count = 0;
   
	for( QueueNode temp = start; temp != null; temp = temp.previous )
		count++;
   
	return count;
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: returning nothing Posted: May 10, 2002 7:12 PM
Reply to this message Reply
Hmm, is this method a method of the QueueNode class? If that is the case and you are trying to find the offset of the current node from the start, then I would guess that you'd start from the current node (this) and traverse backward to the start node, keeping count.

By the way, a List implements all the necessary behavior of a Queue, in case you were not aware of it. That would save you a lot of work. The LinkedList implementation is probably the best to use for a Queue, but you could use Vector or ArrayList, if you prefer.

Flat View: This topic has 2 replies on 1 page
Topic: FileBrowser in web page Previous Topic   Next Topic Topic: Increasing ball speed & changing position of start - pls help!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use