Issue
I am new in java I have a problem the for cycle is not adding data to the List:
this is the code
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(Model modl) throws TwitterException {
// --------------------------------------------------
// getTweets example
// --------------------------------------------------
Twitter twitter = new TwitterFactory().getInstance();
/*
* final TweetsResponse tweets = GetTweetsKt.getTweets(twitter, new
* long[]{1510021574505738201L}, null, null, null, null, null, "");
*/
/*
* userId: Long, endTime: Date? = null, exclude: String? = null, expansions:
* String? = null, maxResults: Int? = null, mediaFields: String? = null,
* paginationToken: String? = null, placeFields: String? = null, pollFields:
* String? = null, sinceId: Long? = null, startTime: Date? = null, tweetFields:
* String? = null, untilId: Long? = null, userFields: String? = null,
*/
long userId = 1510021146334437310L;
Date endTime = null;
String exclude = null;
String expansions = null;
int maxResults = 100;
String mediaFields = null;
String paginationToken = null;
String placeFields = null;
String pollFields = null;
long sinceId = 1510021521342926810L;
Date startTime = null;
String tweetFields = null;
long untilId = 1510022761187627010L;
String userFields = null;
final TweetsResponse tweets = TimelinesExKt.getUserTweets(twitter, userId, endTime, exclude, expansions,
maxResults, mediaFields, paginationToken, placeFields, pollFields, sinceId, startTime, tweetFields,
untilId, userFields);
tweetModel tweetmodel = new tweetModel();
List<tweetModel> tweetmodelList = new ArrayList<>();
for (int i = 0; i < tweets.getTweets().size(); i++) {
System.out.println(i);
System.err.println("FOR TWEETS = " + tweets.getTweets().get(i).getText().toString());
tweetmodel.setTweets(tweets.getTweets().get(i).getText().toString());
this.tweetmodel2.setTweets(tweetmodel.getTweets().toString());
tweetmodelList.add(this.tweetmodel2);
System.err.println("TWEETMODEL2::::::::::::::::"+this.tweetmodel2.getTweets().toString());
System.err.println("//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets=="
+ tweetmodel.getTweets().toString());
}
for (int z = 0; z < tweetmodelList.size(); z++) {
System.err.println(" ====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ======== " + tweetmodelList.get(z).getTweets().toString());
}
modl.addAttribute("tweetlists", tweetmodelList);
return "index";
}
this is the line of code where I set the data fetched by getTweets() function
tweetmodel.setTweets(tweets.getTweets().get(i).getText().toString());
this.tweetmodel2.setTweets(tweetmodel.getTweets().toString());
tweetmodelList.add(this.tweetmodel2);
the problem is that tweetmodelList is showing and recording the last recordset, I have 7 tweets and the function writes 7 times the last record, do not know what I am doing wrong, since
this.tweetmodel2.setTweets(tweetmodel.getTweets().toString());
tweetmodelList.add(this.tweetmodel2);
is inside the for cycle so it should iterate.... for example and for better understanding of what happens I will post here the results printed in the console
data in console HERE
FOR TWEETS = tweet 6
TWEETMODEL2::::::::::::::::tweet 6
//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets==tweet 6
1
2
3
4
5
6
FOR TWEETS = hello
TWEETMODEL2::::::::::::::::hello
//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets==hello
FOR TWEETS = cinco tweets
TWEETMODEL2::::::::::::::::cinco tweets
//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets==cinco tweets
FOR TWEETS = no
TWEETMODEL2::::::::::::::::no
//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets==no
FOR TWEETS = si
TWEETMODEL2::::::::::::::::si
//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets==si
FOR TWEETS = tweet number 3
TWEETMODEL2::::::::::::::::tweet number 3
//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets== tweet number 3
FOR TWEETS = tweet last
TWEETMODEL2::::::::::::::::tweet last
//////////////////TWEET MODEL HAVE THIS!!!!!!! tweetmodel.settweets==programar es mi tweet last
====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ======== tweet last
====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ======== tweet last
====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ======== tweet last
====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ======== tweet last
====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ======== tweet last
====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ======== tweet last
====== TWEETS IN TWEETMODELIST AFTER FIRST FOR tweetmodelList.add(tweetmodel); ========
tweet last
as you can see for some reason TWEETMODELIST only saves the last registry but saves the same the correct amount of tweets fetched which are 7 the problem is that as you can see, the other 7 tweets are different and are not being recorder or saved by the
tweetmodelList.add(this.tweetmodel2);
thanks in advance, the correct answer will be marked as solved
Solution
Firstly, the next statements are executed inside the for
loop:
tweetmodel.setTweets(tweets.getTweets().get(i).getText().toString());
this.tweetmodel2.setTweets(tweetmodel.getTweets().toString());
What it does is to replace the tweets collection of tweetModel
, and then it replaces the tweets collection of tweetModel2
.
But when you add this.tweetmodel2
into tweetmodelList
you're not adding the collection of tweets that holds this.tweetmodel2
. Instead, you're adding the reference to this.tweetmodel2
itself.
Then, after each iteration of the for
loop, the instance of this.tweetmode2
is the same, but its collection of tweets is not (becase its replaced with the new one). So, after 6 iterations you have only one instance of this.tweetmode2
and the last tweets replaced. And that's why you see the last tweet printed out.
Since the for
statement iterates 6 times, and you have the next statement inside, then it has added 6 elements into the variable tweetmodelList
.
tweetmodelList.add(this.tweetmodel2); <-- adds the reference to variable tweetmodel2 into tweetmodelList 6 times
It can be fixed by creating and assigning a new instance to this.tweetmodel2
like following:
tweetmodel.setTweets(tweets.getTweets().get(i).getText().toString());
this.tweetmodel2 = new TweetModel2(); <-- Create and assign a new instance
this.tweetmodel2.setTweets(tweetmodel.getTweets().toString());
tweetmodelList.add(this.tweetmodel2);
Answered By - Leo Tapia
Answer Checked By - Terry (JavaFixing Volunteer)