3 Replies - 1288 Views - Last Post: 12 October 2010 - 05:55 PM Rate Topic: -----

#1 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 110
  • Joined: 04-December 09

How to Print Runtime Stack?

Posted 12 October 2010 - 05:41 PM

hi, i have to print my runtime stack(not the compiler, one i made up) and i'm not sure if my algorithm is correct:

suppose i have a stack called stackFrames that holds the integer positions of each frame in the stack (each value is start position of a frame)

also i have a vector called runTimeStack that holds all the values in each position of the stack

for example: if runTimeStack had: 0 1 4 2 9 8 7

and stackFrames had: 0 3

then two frames begin at index 0 and index 3 of runTimeStack, so the two frames are: [0,1,4] and [2,9,8,7]

but i have no idea how to print out my runTimeStack and separate it by frames..
here is my attempt:

  for (int i = 1; i < stackFrames.size(); i++) {
            int elements = stackFrames.elementAt(i + 1) - stackFrames.elementAt(i);  
            System.out.print("[");   
            for (int j = 0; j < elements; j++) {
                if (j == elements - 1) {
                    System.out.print(runTimeStack.get(j));
                } else {
                    System.out.print(runTimeStack.get(j) + ",");
                }
            }
            System.out.print("]");
        }


my code seems very bad and wrong.. is there an easier way to do this or can i have some tips?

Is This A Good Question/Topic? 0
  • +

Replies To: How to Print Runtime Stack?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to Print Runtime Stack?

Posted 12 October 2010 - 05:46 PM

Why not but instead of

          for (int j = 0; j < elements; j++) {
              if (j == elements - 1) {
                  System.out.print(runTimeStack.get(j));
              } else {
                  System.out.print(runTimeStack.get(j) + ",");
              }
          }


I would
          System.out.print(runTimeStack.get(0));
          for (int j = 1; j < elements; j++)
              System.out.print(", " + runTimeStack.get(j));


Was This Post Helpful? 1
  • +
  • -

#3 musiliu  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 110
  • Joined: 04-December 09

Re: How to Print Runtime Stack?

Posted 12 October 2010 - 05:49 PM

oh..thanks i didn't think of that

so my code is actually correct?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: How to Print Runtime Stack?

Posted 12 October 2010 - 05:55 PM

View Postmusiliu, on 12 October 2010 - 06:49 PM, said:

oh..thanks i didn't think of that

I think it is lighter

Quote

so my code is actually correct?

If it prints what it is supposed to print, I guess so :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1