Solved

Problems With Linked List

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example issue 2 25
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 41
Java array 21 79
Java pass by reference 3 13
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

735 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