Results 1 to 3 of 3
- 12-01-2010, 10:21 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Problems with method which returns result
public T getElement(){
T result = null;
int index = 0;
if (usedElements > 0){
index = new Random().nextInt(data.length);
result = (T)data[index];
if (data[index] == null){getElement();}
removeElemet((T)data[index]);
}
return result;
}
Hello there.
I have cuted one fragment of my program, the main idea is to take elements from array randomly and then delete them. removeElement(); searches for element in array and deletes it after it is randomly chosen by getElement();
After removeElement(); control goes to return function which must return result, but sometimes return gives control back to " if (data[index] == null){getElement();} ", it happens randomly, is there any explanation of such?
- 12-01-2010, 10:55 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Use code tags when posting code, please.
Makes things easier to read.
That return statement cannot result in the flow going to the earlier if statement.Java Code:public T getElement(){ T result = null; int index = 0; if (usedElements > 0) { index = new Random().nextInt(data.length); result = (T)data[index]; if (data[index] == null) { getElement(); } removeElemet((T)data[index]); } return result; }
So something else is going on.
Stick a ton of println()s in there so you can follow the flow.
- 12-01-2010, 12:16 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
How do I write a method that returns an array where ith element contains a[i] + b[i]?
By CaptainBlood in forum New To JavaReplies: 5Last Post: 10-30-2010, 12:05 AM -
What is the Java library method that takes String parameter and returns a Boolean?
By CaptainBlood in forum New To JavaReplies: 2Last Post: 10-15-2010, 05:09 AM -
Method problems
By Turismo in forum New To JavaReplies: 8Last Post: 07-18-2010, 06:57 AM -
Problems with method
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 06-28-2007, 06:49 PM -
Problems with Find method in EJB
By Nick15 in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 05-14-2007, 01:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks