0

I need to transfer all the data I got in ArrayList to LinkedList and display all it all just like I did with ArrayList. I failed to display it out when I got the data transferred to LinkedList. The code is as below :

import javax.swing.*;
import java.util.*;

public class testEmployee
{
    public static void main (String [] args)
    {
        ArrayList <Employee> empArray = new ArrayList();
        LinkedList yrIncm = new LinkedList();

    Employee emp;
    int empNum;
    boolean found = true;

    empNum = Integer.parseInt(JOptionPane.showInputDialog ("How many employees information do you want to store?"));

    for (int i = 0; i < empNum; i++)
    {
        String sEmpId = JOptionPane.showInputDialog ("Please enter the employee's ID");
        String sEmpName = JOptionPane.showInputDialog ("Please enter the employee's Name");
        String sEmpPosition = JOptionPane.showInputDialog ("Please enter the employee's position");
        Double dSalary = Double.parseDouble (JOptionPane.showInputDialog ("Please enter the employee's monthly salary"));

        emp = new Employee (sEmpId, sEmpName, sEmpPosition, dSalary);

        empArray.add (emp);
    }

    System.out.println ("Employee that obtains a monthly salary more than RM 2000.00");
    System.out.println ("===========================================================");
    for (int i = 0; i<empArray.size(); i++)
    {
        if (empArray.get(i).getSalary() > 2000)
        {
            empArray.get(i).display(); // This will display the info using ArrayList
        }
    }

    System.out.println ("\nEmployee that have yearly income greater than RM 80,000");
    System.out.println ("=======================================================");
    for (int i = 0; i<empArray.size(); i++)
    {
        if ((empArray.get(i).getSalary() * 12) > 80000)
        {

            yrIncm.add (empArray.get(i)); // Is this the correct way of transferring the data? 

            System.out.println (yrIncm); // How do you print it all back? 
        }
    }

   }
}

The display() in class Employee:

public void display()
{
    System.out.println ("\nEmployee's ID : " + sEmpId);
    System.out.println ("Employee's Name : " + sEmpName);
    System.out.println ("Employee's Position : " + sEmpPosition);
    System.out.println ("Employee's Salary : RM " + df.format (dSalary));
}

I'm unable to use the method display() to print it out from the LinkedList. Any help would be appreciated

2
  • 1
    If you want to display a full list of your objects, you should not use display(). You should instead override the toString() method in Employee. You can then print a list and all its elements will be properly printed. Commented Sep 20, 2015 at 12:07
  • note: if this is not home-work that explicitly requires using linked list, then there are very few reasons to prefer this data structure over array list. Commented Sep 20, 2015 at 12:10

2 Answers 2

1
for (int i = 0; i<empArray.size(); i++)
{
    if ((empArray.get(i).getSalary() * 12) > 80000)
    {
        LinkedList yrIncm = new LinkedList();

You create a new linked list each time and put exactly one element into it. Then you print out the one-element list and toss it away.

While the above is certainly wrong, I don't see where you imagine display() entering this picture. Your linked list never leaves the for-loop.

Sign up to request clarification or add additional context in comments.

2 Comments

What a silly mistakes, I've corrected it. Now for the display(), as you can see empArray.get(i).display(); will call display() and print it out, and I've done it with Arraylist. Now I want to do the same with the LinkedList that I created and it seems that I can't call the method to print the exact same info. How do I make it possible?
Java collections are iterated over using an Iterator returned from the iterator() method. A shorthand notation for that is the enhanced for loop: for (Employee emp : listOfEmployees) { emp.display(); } You should use this instead of get(i), even for ArrayList.
1

You can use LinkedList.addAll to get all the elements of the ArrayList.

LinkedList<Employee> yrIncm = new LinkedList();

yrIncm.addAll(empArray);
yrIncm.forEach(employee -> employee.display());

The problem with your code is that you are re-creating the LinkedList everytime and probably trying to call display() on a linkedlist instead of employee oobject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.