0

I am currently working on an object-structure exercise where I implement a simple Twitter-interface. I have a TwitterAccount object and a Tweet object. The TwitterAccount object has a Stack that contains all the tweets. In my getTweet(index)-method, I want the newest tweet to be returned with getTweet(1), and second-newest with getTweet(2)... etc.

I do this by creating a temporary tweet-stack, pop index-1 Tweets off the temporary stack, then return the next one. My issue is that the original tweets stack also has it's elements popped. Did I create a pointer to the tweets stack when I created the temporary stack?

Stack<Tweets> tweets;

(...)

public Tweet getTweet(int index) {

    Stack<Tweet> tempTweets = tweets;
    for (int i = 0; i < index-1; i++) 
        tempTweets.pop();

    return tempTweets.pop();

}
vegarab
  • 309
  • 1
  • 10
  • 2
    "*Did I create a pointer to the tweets stack when I created the temporary stack?*" ==> yes. – assylias Apr 25 '17 at 14:38
  • Unless you are not creating a new instance, yes, you are looking at the same stack object. – JDC Apr 25 '17 at 14:38
  • Yes for sure you are – Basil Battikhi Apr 25 '17 at 14:39
  • How could I implement the method to not do that? Whenever I have used this way of implementing it with other data-types, it has NOT created a pointer. – vegarab Apr 25 '17 at 14:39
  • Stack tempTweets = new Stack(); tempTweets = this.tweets; This gives the same thing. How do I copy tweets by value and not by reference? – vegarab Apr 25 '17 at 14:43
  • @VegarAndreasBergum I think you want to clone the Stack. The simplest method would be to use the existing clone() method of the Stack. Although i'm not sure if that gives you a shallow copy (New Stack pointing to same objects as other list) or a deep dopy (new stack where all content was cloned as well). You should check out the JavaDoc of the clone method. – OH GOD SPIDERS Apr 25 '17 at 14:47
  • @OHGODSPIDERS Thanks, that is what I was looking for. – vegarab Apr 25 '17 at 14:47
  • 1
    @VegarAndreasBergum `Stack tempTweets = (Stack) tweets.clone();` – Michael Apr 25 '17 at 14:52
  • @OHGODSPIDERS After further investigation I see that clone(): "Returns a clone of this vector. The copy will contain a reference to a clone of the internal data array, not a reference to the original internal data array of this Vector object.". From what I can see, it doesn't create a copy of the stack, but rather the contents of the stack. What I don't understand is why the get() method did not work? Doesn't a Stack inherit that from the Vector-object? – vegarab Apr 25 '17 at 14:53
  • @Michael This! Just Stack instead of Integer. Worked like a charm. – vegarab Apr 25 '17 at 14:55
  • @VegarAndreasBergum Because [Java.util.Stack is crap](https://keithwilliamstechblog.wordpress.com/2016/02/13/why-the-java-stack-class-is-bad/). – Michael Apr 25 '17 at 15:00

2 Answers2

1

Yes, in this case you're just looking at the same object. There is no copying happening here.

The easiest way to create a copy of a Stack is to clone it.

Stack<Tweet> tempTweets = (Stack<Tweet>) tweets.clone();

PS Consider using Deque instead because Stack is rubbish

Community
  • 1
  • 1
Michael
  • 41,989
  • 11
  • 82
  • 128
0

With code

 Stack<Tweet> tempTweets = tweets;

you create reference to the tweets object, so whatever you change with tempTweets e.g.

tempTweets.pop();

is also changed with tweets

If you want to make a copy of an object you can use clone method from java Object. But it have to be implemented in Stack.

It's hard to say more without knowing objects you are using.

Loginus
  • 151
  • 8