gpt4 book ai didi

java - 如何将对象从ArrayList传输到LinkedList

转载 作者:行者123 更新时间:2023-12-02 04:21:58 25 4
gpt4 key购买 nike

我需要将 ArrayList 中获得的所有数据传输到 LinkedList 并显示所有数据,就像我对 ArrayList 所做的那样。当我将数据传输到LinkedList时,无法显示出来。代码如下:

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?
}
}

}
}

Employee类中的display():

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));
}

我无法使用方法 display() 从 LinkedList 中打印出来。任何帮助将不胜感激

最佳答案

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

每次都会创建一个新的链表,并只将一个元素放入其中。然后打印出单元素列表并将其扔掉。

虽然上面的内容肯定是错误的,但我不明白您想象的 display() 在哪里进入这张图片。您的链接列表永远不会离开 for 循环。

关于java - 如何将对象从ArrayList传输到LinkedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com