Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By ozzyman

Thread: Stack Elements

  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Stack Elements

    Hi,

    In a Stack of String elements, how can you go about comparing two elements to determine their alphabetical order?

    For an example :
    Stack elements - Tuesday, Friday.

    alphabetical Stack order - Friday, Tuesday

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Stack Elements

    What methods does the Stack class have for accessing its contents? You would have to use some of them to get at what is in the Stack.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Stack Elements

    The only method that seems to do with its contents is the search() method, which only determines where an element is the Stack. It also inherits alot of methods which has ways of accessing its contents from the Vector class. But I'm not sure how I can compare Stack elements specifically?
    Thanks

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Stack Elements

    Are you trying to do this yourself or are you allowed to use stuff that's already available in Java? You can compare/sort elements in a collection using Collections.sort and a custom comparator.

    See this tutorial for more info:
    Object Ordering (The Java™ Tutorials > Collections > Interfaces)

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Stack Elements

    Pop them off the Stack and you can look at them.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Stack Elements

    Well yea I can use stuff in the Java API, which I'd prefer to do anyway. But its Stack objects I'm trying to compare, not Collection objects.

    Oh you mean pop them off the Stack and assign them into a String array of objects, and them use the compareTo() method, and then assign the String array back onto the Stack?

    Thanks

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Stack Elements

    You can create your own comparator to compare any object you like... what do you mean Collection objects? a Collection can hold any object... its just a collection...

    If you prefer to reuse code then you should probably read up on the link I posted before.
    dougie1809 likes this.

  8. #8
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Stack Elements

    Oh yeah I understand now. I've tried to add the Stack of dvds into a Collection called dvdCollection, which seems fine. I then tried to use the compare(object, object) method, but can't get my head around it?
    The snippet code it below:

    Java Code:
    //loop through dvds Stack
    		for(int i=0;i<=dvds.size();i++)
    		{
    			//Create and initalize a Collection object
    			Collection dvdCollection =null;
    			//Add the dvds Stack to dvdCollection
    			dvdCollection.add(dvds);
    			//Trying to compare the first two Stack elements, and so on
    			compare(dvds(i), dvds(i+1));
    		}

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Stack Elements

    What does the compare method do? If it returns a value, your code is not using it.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Stack Elements

    Yes it returns an integer, but I'm not concerned what it returns, I just want to know if I'm using the compare method correctly? I have added a Stack list into a Collection list, so I can use the compare method, but I can seem to use it correctly.
    Thanks

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Stack Elements

    If you are not using the value returned by compare, then you are not using it correctly.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Stack Elements

    Ok. Using that method, how can I compare objects specified by their indexes in the for loop? That's all I'd like to know.
    Thanks

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Stack Elements

    What class is the compare method in? Can you post the code for the compare method if it is your method?
    Also you don't show what dvds is. The syntax: dvds(i) is the syntax for a method. The syntax: dvds.size() looks like dvds is some kind of object reference.
    Last edited by Norm; 03-25-2012 at 06:04 PM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Stack Elements

    Dougie, you can create a Comparator with a generic type and then override the compare(T obj1, T obj2) method to compare two of your objects (of type T).

    You just need to tell the Comparator how to compare two objects, by returning 0, -1 or 1. 0 means the two objects are equal and don't need to be rearranged. 1 means obj1 > obj2 and -1 means obj2 > obj1.

    You can use any object properties to make your return decision (0, 1, or -1).

    Here is an example for you:

    Java Code:
    class Product {
        // Create a Comparator to sort product prices
        // while using generics with class Product
        public static final Comparator<Product> PRICE_DESC = new Comparator<Product>() {
    
            public int compare(Product p1, Product p2) {
                double price1 = p1.getPrice();
                double price2 = p2.getPrice();
                if (price1 > price2) {
                    return 1;
                } else if (price2 > price1) {
                    return -1;
                } else return 0;
            }
    
        }
    }
    Because this comparator checks which price is greater, it should sort the products with prices in descending order.

    Now, whenever you have a Collection of Product objects, you can sort them like this:

    Java Code:
    // Create a collection of Products
    List<Product> productList = new ArrayList<Product>();
        // add Products
        productList.add(new Product(...));
        ...
    
    // Sort the list by price
    Collections.sort(productList, Product.PRICE_DESC);
    Then you can print the list to check the new order.

  15. #15
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Stack Elements

    Thanks alot ozzyman. I know how your example may be pseudocode. But the line
    Java Code:
    public static final Comparator<Product> PRICE_DESC = new Comparator<Product>()
    cannot be instantiated, where Comparator is an abstract class?

    And the way you have your overrided compare method is fine. Where in my case I'l be comparing String's, so is this correct below:?
    Thanks

    Java Code:
    public int compare(Dvd dvd1, Dvd dvd2)
    	{
    		return dvd1.getTitle().compareTo(dvd2.getTitle());
    	}

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Stack Elements

    Have you tried it to see what happens?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 01-14-2012, 09:56 PM
  2. need stack help
    By ali1 in forum New To Java
    Replies: 4
    Last Post: 11-05-2011, 09:48 PM
  3. Replies: 3
    Last Post: 10-20-2011, 12:42 AM
  4. Stack
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 03-04-2011, 09:14 PM
  5. How big is the stack??
    By mgeno216 in forum New To Java
    Replies: 6
    Last Post: 03-04-2011, 09:04 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
  •