Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3596936
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:03:12+00:00 2026-05-18T20:03:12+00:00

I have used linked lists before with Strings, doubles, etc., and they always worked

  • 0

I have used linked lists before with Strings, doubles, etc., and they always worked exactly as expected. But now I am forming a linked list of items, and whenever I add a new element to the list, all objects in the list apparently become equal to the last object.

The essential code is as below:

import java.util.*;

public class Global
{

  static public LinkedList<StockInfo> DiaryStocks = new LinkedList<StockInfo>();

  static public class StockInfo    //info related to each stock in diary
  {
    String recordDate;             
    String ticker;
    int   status;                  
    String buyDate;
    String sellDate;
    double buyPrice;
    double sellPrice;
    double nmbrShares;
  }//StockInfo

//The following function places the Diary data for a stock in the arraylist

 static public void AddDiaryData(StockInfo thisdata)
  {
    String tckr;
    int i;

    DiaryStocks.add(thisdata);

    for (i = 0; i < DiaryStocks.size(); i++)   //this is debug code
    {
      tckr = DiaryStocks.get(i).ticker;
    }
  } 
}

As I said, when single stepping through the debug code near the bottom, each time I add a new item to the list, the list size grows as it should, but the tckr item only corresponds to the last item added.

Any insights into this puzzle would be greatly appreciated.

John Doner

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:03:13+00:00Added an answer on May 18, 2026 at 8:03 pm

    From the symptoms you are describing, it seems as if you are always adding a reference to the same StockInfo object instance to your list, rather than a reference to a new copy each time.

    When that object is updated with the contents of the new entry, all list entries appear to change to reflect that latest entry.

    This problem lies outside the code snippet that you posted, perhaps in the caller of the AddDiaryData method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used linked lists before with Strings, doubles, etc., and they always worked
I have made a few linked lists before and the concepts all make sense
We have used Infragistics controls in our applications for years. However, we have always
I'm having some issues with my linked list. I have struct dListNode used as
I have only used 3 functional languages -- scala, erlang, and haskell, but in
I'm sure some of you already experienced it. I have two linked lists of
I need to merge two doubly-linked lists, but not by their values (the lists
I use a lot of lists and arrays but I have yet to come
I have used strtok to tokenize my input file and I need a linked
I have used the following as comments in my .emacs // {{{ // }}}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.