gpt4 book ai didi

java - 如何修复堆结构的循环

转载 作者:行者123 更新时间:2023-12-02 13:18:32 24 4
gpt4 key购买 nike

代码可以编译,但是当您运行它时,会出现错误消息,给出空指针异常。正如底部所见。该代码应该从程序中输入的 txt 文件中读取文本,然后创建一个新的 txt 文件,其中第一个 txt 文件的内容按服务年限排序。但是,我不断收到该错误消息。任何帮助将不胜感激。我在底部添加了错误消息,感谢所有帮助您的时间和精力的人:)

(25分)定义一个名为Employee的Java类。该类有数据成员以及以下六个数据项中每一项的随附访问器和修改器方法。 (这涉及创建文件 Employee.java。)

  • id(字符串)
  • 名称(字符串)
  • 工资(双倍)
  • 部门(字符串)
  • 位置(字符串)
  • 服务年限(整数)(25 分)创建一个文本(数据)文件,其中包含至少五个不同的数据员工(对象)。让每个数据项位于其自己的行中文本文件。例如,文件的前六行可能如下所示:

    086244

    莎莉·史密斯

    100000.00

    会计

    经理

    7

(50分)“堆”是一种满足堆性质的基于树的数据结构。最大堆是一个完全二叉树,其中每个内部节点中的值都大于或等于该节点的子节点中的值。

通过拥有堆(或满足堆属性的数组),在数组上执行重要任务(例如查找数组中的最大元素(并将其删除)和排序)会更有效(通常更快)数组。

在本作业中,您必须编写一个程序来从文件中读取员工列表。文件的名称为“Employee.txt”。程序应将排序数组输出到名为“SortedEmployee.txt”的文件中堆排序代码:

public class HeapSort
{

//heap sort method
public static <Employee extends Comparable<Employee>> void heapSort(Employee[] list)
{
//create a Heap of integers
Heap<Employee> heap = new Heap<>();

//add elements to the heap
for (int i = 0; i< list.length; i++)
heap.add(list[i]);

//remove elements from the heap
for(int i = list.length - 1; i >= 0; i--)
list[i] = heap.remove();
}


}

堆代码:

import java.util.ArrayList;

public class Heap<Employee extends Comparable<Employee>>
{
private ArrayList<Employee> list = new ArrayList<>();

public Heap(){}

public Heap(Employee[] objects)
{
for(int i = 0; i < objects.length; i++)
add(objects[i]);
}
public void add(Employee newObject)
{
list.add(newObject);
int currentIndex = list.size() - 1;

while(currentIndex > 0)
{
int parentIndex = (currentIndex -1)/2;
if(list.get(currentIndex).compareTo(list.get(parentIndex)) > 0)
{
Employee temp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, temp);
}
else
break;

currentIndex = parentIndex;
}
}
public Employee remove()
{
if(list.size() == 0) return null;

Employee removeObject = list.get(0);
list.set(0, list.get(list.size() -1));
list.remove(list.size() -1);

int currentIndex = 0;
while(currentIndex < list.size())
{
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
if(leftChildIndex >= list.size()) break;
int maxIndex = leftChildIndex;
if(rightChildIndex < list.size())
{
if(list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0)
maxIndex = rightChildIndex;
}
if(list.get(currentIndex).compareTo(list.get(maxIndex)) < 0)
{
Employee temp = list.get(maxIndex);
list.set(maxIndex, list.get(currentIndex));
list.set(currentIndex, temp);
currentIndex = maxIndex;
}
else
break;
}
return removeObject;
}
public int getSize()
{
return list.size();
}

public void print()
{
for (int i = 0; i <= getSize()-1; i++)
{
System.out.print("Index: " + i + " Data: " + list.get(i));

System.out.println();
}
}
}

员工对象类:

public class Employee implements Comparable<Employee>
{
private String id;
private String name;
private double salary;
private String department;
private String position;
private int yos;
public Employee(String id, String name, double salary,String department,String position,int yos)
{
this.id = id;
this.name = name;
this.salary = salary;
this.department = department;
this.position = position;
this.yos = yos;
}
public void setid(String id)
{
this.id = id;
}
public void setname(String name)
{
this.name = name;
}
public void setsalary(double salary)
{
this.salary = salary;
}
public void setdepartment(String department)
{
this.department = department;
}
public void setposition(String position)
{
this.position = position;
}
public void setyos(int yos)
{
this.yos = yos;
}
public String getid()
{
return id;
}
public String getname()
{
return name;
}
public double getsalary()
{
return salary;
}
public String getdepartment()
{
return department;
}
public String getposition()
{
return position;
}
public int getyos()
{
return yos;
}
public int compareTo(Employee emp)
{
return (this.yos - emp.yos);
}
public String toString()
{
return "ID=" + this.id+ ", name=" + this.name+ ", salary= $" + this.salary+ ", department:" + this.department+ ", postion:" + this.position+ ",yos= $" + this.yos + "]\n";
}
}

演示代码:

 import java.util.*;
import java.io.*;


public class EmployeeDemo
{
public static void main(String[] args)throws IOException
{
Employee[] list = new Employee[5];
Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter the text file: ");
String fileName = keyboard.nextLine();

File myFile = new File(fileName);
Scanner inputFile = new Scanner(myFile);

//Read all of the values from the file
//and calculate their total


//Read a value from the file
String id = inputFile.nextLine();
String name = inputFile.nextLine();
double salary = inputFile.nextDouble();
String clear = inputFile.nextLine();
String department = inputFile.nextLine();
String position = inputFile.nextLine();
int yrService = inputFile.nextInt();
String llear = inputFile.nextLine();

list[0] = new Employee(id,name,salary,department,position,yrService);






//close the file


// File o = new File("SortedEmployee.txt");
//o.createNewFile();
System.out.println("Enter the file name to be transfered to: ");
String filename = keyboard.nextLine();
PrintWriter outputFile = new PrintWriter(filename);//dont need the top


//HeapSort<Employee> h = new heapSort<Employee>(Employee);
HeapSort.heapSort(list);

//Display the sum of the numbers
while(inputFile.hasNext())//this loop is wrong too
{

outputFile.println(list[0].toString());

}
outputFile.close();
inputFile.close();
System.out.print("File Sorted and Transferred");

}
}

这是我收到的错误消息:

Please enter the text file: 
C:\Users\jose385\Desktop\Employee.txt
Enter the file name to be transfered to:
green
Exception in thread "main" java.lang.NullPointerException
at Heap.add(Heap.java:22)
at HeapSort.heapSort(HeapSort.java:13)
at EmployeeDemo.main(EmployeeDemo.java:50)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

最佳答案

您将List的大小设置为5

Employee[] list = new Employee[5];

但只添加一个元素

list[0] = new Employee(id,name,salary,department,position,yrService);

实际上只对一个元素进行排序有什么意义

还尝试遵循有关实现Comparable的正确方法的教程

关于java - 如何修复堆结构的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670194/

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