• Post Reply Bookmark Topic Watch Topic
  • New Topic

Quick Question, general type list  RSS feed

 
Chris Hericks
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been programming in Java for about a year now, and there are still somethings that confuse the heck out of me. Part of an assignment I am working to complete instructs to create a List class that will operate with the functionality of a stack. To that end, I have created a class, and am attempting to give the class a generic type so that when I create a structure of this type, I can assign the parameter type of the structure as normal. I have done quite a bit of research on the internet into creating general-type list classes, and it is quite confusing to me. Here is what I have so far:

@SuppressWarnings("unchecked")
public class ListStack<K> implements Stack<K> {

private ListStack<K>[] theStack;

// Error on below line on token ">", Identifier expected after token.
public ListStack<K>() {
theStack = new ListStack[1];
}

Can someone give me some insight to ensure that I am going about this correctly? I just need to get the constructor and beginnings right and I should be able to finish this myself.

Again, the ultimate goal is to be able to call:
Stack<Integer> args = new ListStack<Integer>();
without any errors.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The skeleton of the class seems ok to me. But what's inside really doesn't look like what you want.

You introduced key parameter in class definition, so you need to omit it from constructor signature:


Also, this line seems rather strange to me:

What you declared is array of ListStack instances, or simply array of stacks. Doesn't that sound strange - stack that contains array of stacks?
But I suppose you wanted to declare an underlying structure to hold elements on your stack. In case of your class (if you had semantics in mind when naming your class) it should be list of some kind.

There was similar topic not long ago, so you might want to check it.
 
Chris Hericks
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The goal of the assignment is to create a class which will support the functionality of a Stack. The assignment was required to be called "ListStack", because it is originally supposed to be a bounded List, which is given the actions/personality of a Stack. I was trying to create an array of the ListStack, so that when I reference it in later methods such as push/pop/peek, I will be able to reference the array. Should I use "this" instead? I thought that I was completing the assignment correctly be creating that array.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Hericks wrote:I was trying to create an array of the ListStack


That doesn't make any sense. That says that you'll have an array, and each element of that array will be a ListStack. So you'll have multiple ListStacks, and each ListStack will contain whatever items have been pushed onto it.

So are you supposed to have one stack or many?

, so that when I reference it in later methods such as push/pop/peek, I will be able to reference the array. Should I use "this" instead?


Again, this makes no sense at all. No idea what you're asking here or what you're trying to do.

You may in fact end up using an array. Your implementation of ListStack may use an array to store its elements, but that would be an array of your element type, not an array of ListStack.
 
Chris Hericks
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And by pointing that you, you just helped me figure it out, LOL. Thanks guys. I was being stupid.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got it sorted out!

And you're on the right track. A not insignificant part of successful programming comes from, "Oh! Duh! What was I thinking?!?"
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!