Results 1 to 12 of 12

Thread: Stack indexes?

  1. #1
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Unhappy Stack indexes?

    So all java data structures report indexes based on a zero based position, so first element is index 0, second element is index 1 and so on. I finished learning about vectors and i am now learning about stacks but the index system is confusing me. Look at the code.

    Java Code:
    import java.util.Stack;
    
    public class Example {
    	public static void main (String[] args) {
    		Stack s = new Stack();
    		s.push("A");//2 index
    		s.push("B");//1 index
    		s.push("C");//0 index
    
    		 System.out.println(s.search("C")); // prints out 1
    		
    	}
    
    }
    Why isnt printing out 0 instead of 1? I would appreciate any help.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Stack indexes?

    Here is the 5-second of effort you need to inform yourself, for future "how does this work" questions.

    google: java 7 stack
    result: Stack (Java Platform SE 7 )

    Quote:

    "public int search(Object o)

    Returns the 1-based position where an object is on this stack"

  3. #3
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Stack indexes?

    Quote Originally Posted by gimbal2 View Post
    Here is the 5-second of effort you need to inform yourself, for future "how does this work" questions.

    google: java 7 stack
    result: Stack (Java Platform SE 7 )

    Quote:

    "public int search(Object o)

    Returns the 1-based position where an object is on this stack"
    I was reading Sams Teach Youself Java in 21 days and this is what it said
    "The search() method returns the distance from the top of the stack of the element if it is
    found, or -1 if not. In this case, the “Two” string is the third element from the top, so the
    search() method returns 2 (zero-based)."

    I just assumed i was doing something wrong. Thankyou for replying though, ill refer to the java docs more frequently when i have questions in the future.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Stack indexes?

    get a different book, any dummies or "learn XX in XXX" book is by definition horrible. And a lie, because you can't learn Java in 21 days. Its too complicated.
    Last edited by gimbal2; 07-15-2013 at 05:11 PM.

  5. #5
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Stack indexes?

    Quote Originally Posted by gimbal2 View Post
    get a different book, any dummies or "learn XX in XXX" book is by definition horrible. And a lie, because you can't learn Java in 21 days. Its too complicated.
    Im not new to java and i know its not possible to learn java in 21 days. I already went through most of The java tutorial but i felt like i hadn't grasped everything completely so im just reading through the book to refresh my memory and learn some new stuff about java. Would you have a book suggestion though?

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Stack indexes?

    Effective Java 2nd edition is a good bet if you're not new.

    But to be brutally honest I can't really believe that you're not new to Java when you create question threads that can be easily answered by javadocs. That should have been hardwired into your system by now. Javadocs first, Google second, forum last.

    EDIT: I don't mean to be offensive, I just want to point out that perhaps you should be a little more realistic about your current skill level ;)
    Last edited by gimbal2; 07-15-2013 at 05:35 PM.

  7. #7
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Stack indexes?

    Quote Originally Posted by gimbal2 View Post
    Effective Java 2nd edition is a good bet if you're not new.

    But to be brutally honest I can't really believe that you're not new to Java when you create question threads that can be easily answered by javadocs. That should have been hardwired into your system by now. Javadocs first, Google second, forum last.
    Well i never said i was good at java, i just said i wasn't new to it, ive been programing in java on and off for a year now. Ill look into Effective Java 2nd Edition.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Stack indexes?

    It must be an old book too (unless the author is ignoring generics). Stack is a generic type, yet you are declaring it as a raw type. In your example it should be declared as:
    Java Code:
    Stack<String> s = new Stack<>(); // java 1.7 syntax
    I recommend you only use books that cover Java 1.5+. And if you can get a reputable book covering 1.7, then do so.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Liq
    Liq is offline Member
    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    Default Re: Stack indexes?

    Quote Originally Posted by jim829 View Post
    It must be an old book too (unless the author is ignoring generics). Stack is a generic type, yet you are declaring it as a raw type. In your example it should be declared as:
    Java Code:
    Stack<String> s = new Stack<>(); // java 1.7 syntax
    I recommend you only use books that cover Java 1.5+. And if you can get a reputable book covering 1.7, then do so.

    Regards,
    Jim
    Actually he covers generics 10 pages later, and this is the book, would have gotten the 6th edition but i went with this one. It has good reviews and its not to old it is based on java 1.6

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Stack indexes?

    Quote Originally Posted by gimbal2 View Post
    Javadocs first, Google second, forum last.
    What about those of us who use Google to find the Javadocs?
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Stack indexes?

    Quote Originally Posted by Tolls View Post
    What about those of us who use Google to find the Javadocs?
    :)
    Plenty of Javadoc links to be found in the forum too ;)
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default Re: Stack indexes?

    Quote Originally Posted by Tolls View Post
    What about those of us who use Google to find the Javadocs?
    :)
    And what about those poor souls who use the JavaDocs to find Google?

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 01-14-2012, 08:56 PM
  2. Get query indexes.
    By peliukasss in forum Lucene
    Replies: 1
    Last Post: 08-23-2010, 08:11 PM
  3. array indexes
    By Kaito in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 03:14 AM
  4. Removing Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 07:23 AM
  5. Creating Indexes
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 07-17-2009, 07:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •