Results 1 to 16 of 16
Thread: Stack Elements
- 03-23-2012, 02:54 AM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
- 03-23-2012, 03:54 AM #2
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.
- 03-23-2012, 04:00 AM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
-
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)
- 03-23-2012, 04:12 AM #5
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.
- 03-23-2012, 04:17 AM #6
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
-
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.
- 03-25-2012, 02:58 PM #8
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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)); }
- 03-25-2012, 03:18 PM #9
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.
- 03-25-2012, 03:50 PM #10
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-25-2012, 04:22 PM #11
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.
- 03-25-2012, 04:44 PM #12
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-25-2012, 06:01 PM #13
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.
- 03-26-2012, 04:03 PM #14
- Join Date
- Mar 2011
- Location
- London, UK
- Posts
- 797
- Blog Entries
- 2
- Rep Power
- 10
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; } } }
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);
- 03-26-2012, 05:58 PM #15
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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>()
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()); }
- 03-26-2012, 06:07 PM #16
Similar Threads
-
Stack problem. Object array pushed into stack came out different.
By LoViNgHeArTy in forum New To JavaReplies: 2Last Post: 01-14-2012, 09:56 PM -
need stack help
By ali1 in forum New To JavaReplies: 4Last Post: 11-05-2011, 09:48 PM -
Print the sum of elements, determined by zero elements
By Dimitri in forum New To JavaReplies: 3Last Post: 10-20-2011, 12:42 AM -
Stack
By kayln in forum EclipseReplies: 0Last Post: 03-04-2011, 09:14 PM -
How big is the stack??
By mgeno216 in forum New To JavaReplies: 6Last Post: 03-04-2011, 09:04 AM
Bookmarks