Solved

Problems With Linked List

Posted on 2004-03-26
4
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

734 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