Solved

Problems With Linked List

Posted on 2004-03-26
4
204 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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