如何修复堆结构的循环

时间:2017-04-28 00:49:39

标签: java data-structures heap project

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

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

  • id(string)
  • name(string)
  • 薪水(双倍)
  • department(string)
  • 位置(字符串)
  • 服务年限(整数) (25分)创建一个包含至少五种不同数据的文本(数据)文件 员工(对象)。让每个数据项都在自己的行中 文本文件。例如,文件的前六行可能如下所示:

    086244

    莎莉·史密斯

    100000.00

    会计

    管理器

    7

(50分)'Heap'是一个满足堆属性的基于树的数据结构。 max-heap是一个完整的二叉树,其中每个内部节点中的值大于或等于该节点子节点中的值。

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

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

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.

1 个答案:

答案 0 :(得分:1)

您使Employee[] list = new Employee[5]; 的大小为5

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

但只添加一个元素

Comparable

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

还尝试按照正确的方法来实施{{1}}