Solved

Problems With Linked List

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 68
network + 7 73
What is the latest versions eclipse neon 2 120
advertisement module in core php 4 88
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 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…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

21 Experts available now in Live!

Get 1:1 Help Now