Solved

Problems With Linked List

Posted on 2004-03-26
4
201 Views
Last Modified: 2010-03-31
//im creating 3 mailboxes and trying to add them to the post office linked list. but everytime i create a new mailbox it overwrites all of the previous mailboxes
//

  public static PostOffice myPO;
    public static void main(String[] args)
  {
    MultiThreadedServer multiThreadedServer1 = new MultiThreadedServer();
 
  myPO = new PostOffice();
    System.out.println("PostOffice created.");

    Mailbox mb1 = new Mailbox("lauren", "test1");
    System.out.println("Mailbox mb1 created: " + mb1.getUser() + "-" + mb1.getPassword());
    myPO.add(mb1);
    System.out.println("Mb1 added to PO");
    printMailboxInfo();

    Mailbox mb2 = new Mailbox("a", "z");
    System.out.println("Mailbox mb2 created: " + mb2.getUser() + "-" + mb2.getPassword());
    myPO.add(mb2);
     System.out.println("Mb2 added to PO");
     printMailboxInfo();

    Mailbox mb3 = new Mailbox("chris", "test33");
    System.out.println("Mailbox mb3 created: " + mb3.getUser() + "-" + mb3.getPassword());
    myPO.add(mb3);
    System.out.println("Mb3 added to PO");
    printMailboxInfo();

package server;
import java.util.*;

public class PostOffice
{

  public static LinkedList mailboxList ;
  public PostOffice()
  {
    System.out.println("in POSTOFFICE CONSTRUCTOR!!");
    mailboxList = new LinkedList();
  }

  public void add(Mailbox box)
  {
    System.out.println("In PO: adding mailbox: " + box.getUser() + "-" + box.getPassword());
    this.mailboxList.addFirst(box);
  }
}
public class Mailbox
{
  private static String user, password;

  private static LinkedList emailList  ;

  public Mailbox(String uname, String pass)
  {
    this.user = uname;
    this.password = pass;
   emailList = new LinkedList();


  }

  public void addEmailToBox(Email mail)
  {
    System.out.println("In Mailbox, adding mail: " + mail.toString());
    this.emailList.add(mail);
  }
  public String getUser()
  {
    return this.user;
  }
  public String getPassword()
  {
    return this.password;
  }
  public LinkedList getEmailList()
  {
    return this.emailList;
  }
}
0
Comment
Question by:tricks801
  • 3
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 10690965
thats because your using static variables.
try:

 public LinkedList mailboxList ;

  private String user, password;

  private LinkedList emailList  ;
0
 
LVL 92

Expert Comment

by:objects
ID: 10690970
All instances of the same class share the one static var.
0
 

Author Comment

by:tricks801
ID: 10690991
whoops. thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 10690998
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 47
rhino JavaScript import, load 25 82
couple of eclipse 5 35
maven module vs maven project 3 21
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now