For this assignment given to me, I am supposed to move through a linked list of size 1,000,000 iteratively and recursively. I have the iterative part down but I am confused on the recursive part. I have the concept down but I have told my teacher that I cannot recursively do it because I will inevitably get a stack overflow error. He says I should not be getting that problem but I am totally stuck. Any tips would be great as I have no idea what I am doing wrong.
public static void main(String[] args) {
System.out.println("Please enter how many random numbers you want to generate: ");
Scanner prompt = new Scanner(System.in);
int amount = prompt.nextInt();
Random rnd = new Random();
System.out.println("Creating list...");
for (int i = 0; i < amount; i++) {
randomList.add(rnd.nextInt(100));
}
System.out.println("Going through list...");
iterateNumbers();
long startTimeRec = System.nanoTime();
recursiveNumbers(randomList);
long elapsedTimeRec = System.nanoTime() - startTimeRec;
double seconds = (double)elapsedTimeRec / 1000000000.0;
System.out.println("Recursively, the function took " + seconds + " seconds.");
prompt.close();
}
//create the linked list
public static LinkedList<Integer> randomList = new LinkedList<Integer>();
private static void iterateNumbers() {
long startTime = System.nanoTime();
for (int i = 0; i < randomList.size(); i++) {
randomList.remove();
}
long elapsedTime = System.nanoTime() - startTime;
double seconds = (double)elapsedTime / 1000000000.0;
System.out.println("Iteratively, the function took " + seconds + " seconds.");
}
private static void recursiveNumbers(LinkedList<Integer> current) {
if (current.isEmpty() == true) {
return;
} else {
current.removeFirst();
recursiveNumbers(current);
}
}
}
-
What did you get with this code? What is your exact question?innoSPG– innoSPG03/25/2014 01:50:57Commented Mar 25, 2014 at 1:50
2 Answers 2
It's not very hard to understand. When you run your program, the stack area is not enough,so you have a error.
You can try to add the space of the stack.
run the program with the following arguments:
-Xss1280m
It means create the stack with 1280MB, and you will see the result like this:
enter image description here
-
1how would i go about running my program with those arguments? is it possible to make that statement within my code so it would automatically allocate a greater stack area for anyone who runs the program?Arkant0r– Arkant0r03/25/2014 01:59:27Commented Mar 25, 2014 at 1:59
-
1You can compile the java file first with the code
javac Main.java
, and run with the code:java -Xss1280m Main
.And I write the shell script to run the program instead of run it directly.Here is my script:java -Xss1280m Main
,and I just run withsh run.sh
,and it works.If you are in windows, you can also write the .bat file.D0n9X1n– D0n9X1n03/25/2014 02:10:19Commented Mar 25, 2014 at 2:10 -
okay I gotcha, but is there any way to write in my .java file to create a stack with 1280MB?Arkant0r– Arkant0r03/25/2014 02:17:10Commented Mar 25, 2014 at 2:17
-
1I am not sure whether there is any way we can do that.But what I see is a script with a config file until now.D0n9X1n– D0n9X1n03/25/2014 02:24:17Commented Mar 25, 2014 at 2:24
-
1@Arkant0r: I think it is not possible to specify the stack size from the java source code because it would be a security risk.Siyuan Ren– Siyuan Ren03/25/2014 02:57:48Commented Mar 25, 2014 at 2:57
In the iterative version , you should pass in the index at the remove.
randomList.remove(i);
And should you not also repopulate the linked list before you call the recursive method?
for (int i = 0; i < amount; i++) {
randomList.add(rnd.nextInt(100));
}
Not too sure about the Java implementation of the Linked List. Tried looking it up there. Is there a isEmpty() method? Or should you try:
if(current.size() < 1)
return;
-
you're right I forgot to repopulate the list! Let me get back to you once i try that.Arkant0r– Arkant0r03/25/2014 01:51:02Commented Mar 25, 2014 at 1:51
-
Well I repopulated the list and passed the index at the remove statement but I'm still getting a stackoverflowerror when I create a linked list with 1,000,000 integers and then use the recursive function.Arkant0r– Arkant0r03/25/2014 01:56:27Commented Mar 25, 2014 at 1:56
Explore related questions
See similar questions with these tags.