Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

comparing stacks

Thread view: 
Kenneth - 07 Dec 2004 00:09 GMT
how would i compare objects stored in two stacks?

for example the objects are stored in the following manner:

stack 1:

object[1]
object[2]
object[3]

stack 2:

object[4]
object[5]

class object{}{

int a;
int b;
int c;
}

i want to compare variable 'a' of the ojbect in the stack.   how would i
do that?
Ryan Stewart - 07 Dec 2004 00:37 GMT
> how would i compare objects stored in two stacks?
>
[quoted text clipped - 20 lines]
> i want to compare variable 'a' of the ojbect in the stack.   how would i
> do that?

You would get the object from the stack and compare it's "a" value to
whatever you plan on comparing it. If you want a more precise answer, you'll
have to provide some real code and a somewhat better explanation of what
you're looking for.
Kenneth - 07 Dec 2004 08:45 GMT
here is some psuedo code:

Stack stack1 = new Stack();
Stack stack2 = new Stack();

class object{}{

int a;
int b;
int c;

}

for(int i = 0; i <stack1.length; i++)
{
    stack1.push(object[i]);
}
for(int i = 0; i <stack1.length; i++)
{
    stack1.push(object[i]);
}

now how do I compare or assign the objects from stack1 and stack2?

>>how would i compare objects stored in two stacks?
>>
[quoted text clipped - 25 lines]
> have to provide some real code and a somewhat better explanation of what
> you're looking for.
2471 - 07 Dec 2004 10:12 GMT
> here is some psuedo code:
>
[quoted text clipped - 49 lines]
> > have to provide some real code and a somewhat better explanation of what
> > you're looking for.

the best way i can think of is to have your objects impliment
comparable, then you can just use the compareTo method.
Kenneth - 07 Dec 2004 20:25 GMT
>>here is some psuedo code:
>>
[quoted text clipped - 64 lines]
> the best way i can think of is to have your objects impliment
> comparable, then you can just use the compareTo method.

can you show me a sample code?
2471 - 08 Dec 2004 06:52 GMT
> >>here is some psuedo code:
> >>
[quoted text clipped - 66 lines]
>
> can you show me a sample code?

public class objToBeCompared implements Comparable{

String a;

public int getObj{
return a;
}

public int compareTo(Object o){
String comp = o.getObj();

return this.getObj().compareTo(comp.getObj());

}

}

public class CompareStack{

public static void main (String [] args){

Stack stackOne = new Stack();
Stack stackTwo = new Stack();

for(int i = 0; i < args.length; i++){
stackOne.push(args[i]);
}

for(int i = args.length - 1; i > 0; i--){
stackTwo.push(args[i]);
}

//you can only look at the first object of a normal stack
//without removing anything

int compare = stackOne.peep().compareTo(stackTwo.peep());

//compare < 0  if stackOne is less
//compare = 0 if =
//compare > 0 if stackTwo is less
// Might want to check that with the API i always get it
//confused

}

}

i did not compile is so there might be some minor errors, but you get
the idea.
Ryan Stewart - 08 Dec 2004 12:02 GMT
[...]
> String a;
>
> public int getObj{
> return a;
> }
That won't work.

[...]
> int compare = stackOne.peep().compareTo(stackTwo.peep());
And neither will that. It's peek(), not peep(). Not to mentions that stack
returns Object, which doesn't implement Comparable. You'll have to cast or
use generics.

[...]
> i did not compile is so there might be some minor errors, but you get
> the idea.

It's always a good idea to compile before tossing code out. It only takes a
few seconds.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.