• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stacks  RSS feed

 
natsumi jay
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! So sorry if this is in the wrong category.
We are learning stacks in our 121 class right now.
The professor had us take our lab which used ints into a scanner.
However, in the project, the scanner takes in Strings.
I'm just having a lot of trouble converting my methods into strings... (I think??)
The professor pretty much gave us the first half of the coding so I started messing around with it and now I'm lost.

Java is complaining about two things
First is

I think it's because of the third line but after I've messed around with it, java complains so I've left it how it is.

And also the professor wants us to make our own toString method




 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
natsumi jay wrote:Java is complaining about two things
First is

I think it's because of the third line but after I've messed around with it, java complains so I've left it how it is.

What was your intention by writing that line in that way; what do you want it to do?

What you're doing is creating an array of Objects and then through a cast tell the compiler that it should pretend that it's an array with elements of type T. That's not going to work; a cast isn't going to magically convert an Object[] to a T[].

Unfortunately, because of limitations in Java, it's not easy to create an array of a generic type T. The Java compiler will not allow this:

You can do it using reflection, but that's definitely not recommended for beginners. Note that you'll have to pass Class<T> to make this work:

Probably the easiest thing to do is make the member variables 'stack' and 'reverse' an Object[] instead of a T[], and cast elements to T when necessary. (Why do you need 'reverse', by the way?).

natsumi jay wrote:
And also the professor wants us to make our own toString method

Why are you casting to T[] there?
 
natsumi jay
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
I've been considering your input and just changed stack = null
Initially I used it because that's what the professor had in the lab and he had us transfer our code to use for the project so I didn't even think to change it.
Also for the mySearch and isReverse methods.
He wants us to use mySearch to return the location on the stack as if it were a Queue.
For example if you push bottom, middle, top
It'll come out as bottom, middle, top
instead of top, middle, bottom.

I've been tinkering with the code and what if I add a accessing element?
It's my first time using it so I have no idea if I'm doing it correctly but:
Adding

and something like this?


Java is complaining on line 1 on myArrayStack and on line 5 where I use the accessor.

I've been tweaking it another way also. Just experimenting at this point. Please let me know how you feel about this one (haven't finished it):


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!