gpt4 book ai didi

java - 通过传递txt文件创建一个数组并将数组引用变量传递给heapsort方法

转载 作者:行者123 更新时间:2023-12-01 13:00:42 26 4
gpt4 key购买 nike

我的数据结构类(class)项目需要帮助。我假设读取存储在名为 Employee.txt 的文本文件中的 10 名员工数据,并将它们读入一个数组,然后将该数组传递给 heapSort 方法,并将排序后的员工打印到名为 SortedEmployee.txt 的输出文件中。由于某种原因我的程序无法运行。有人可以帮忙吗?

类(class)员工

import java.util.ArrayList;
public class Employee
{
public String empId , empName , empDept , empPos;
public double empSalary;
public int empServ;

Employee()
{
empId = ("");
empName = ("");
empDept = ("");
empPos = ("");
empSalary = 0.0;
empServ = 0;
}
Employee(String id ,String name, double salary, String dept , String pos, int serv)
{
empId = id;
empName = name;
empDept = dept;
empPos = pos;
empSalary = salary;
empServ = serv;
}
public void setId(String id)
{
empId = id;
}
public void setName(String name)
{
empName = name;
}
public void setDept(String dept)
{
empDept = dept;
}
public void setPos(String pos)
{
empPos = pos;
}
public void setSalary(double salary)
{
empSalary = salary;
}
public void setServ(int serv)
{
empServ = serv;
}
public String getId()
{
return empId;
}
public String getName()
{
return empName;
}
public String getDept()
{
return empDept;
}
public String getPos()
{
return empPos;
}
public double getSalary()
{
return empSalary;
}
public int getServ()
{
return empServ;
}
public String toString()
{
String str = "Employee Name : " + empName + "\nEmployee ID : " + empId +
"\nEmployee Deaprtment : " + empDept + "\nEmployee Position : "
+ empPos + "\nEmployee Salary : " + empSalary
+ "\nEmployee Years Served : " + empServ;
return str;
}
public int compareTo(Employee emp)
{
int id = empId.compareToIgnoreCase(emp.empId);
if (id != 0)
return id;
return 0;
}
}

堆排序类

import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;

class zNode
{
private int iData;

public zNode(int key)
{
iData = key;
}
public int getKey()
{
return iData;
}
public void setKey(int k)
{
iData = k;
}
}


class HeapSort
{
private int [] currArray;
private int maxSize;
private int currentSize;
private int currIndex;

HeapSort(int mx)
{
maxSize = mx;
currentSize = 0;
currArray = new int[maxSize];
}

//buildheap
public boolean buildHeap(int [] currArray)
{
int key = currIndex;
if(currentSize==maxSize)
return false;
int newNode = key;
currArray[currentSize] = newNode;
siftUp(currArray , currentSize++);
return true;
}

//siftup
public void siftUp(int [] currArray , int currIndex)
{
int parent = (currIndex-1) / 2;
int bottom = currArray[currIndex];

while( currIndex > 0 && currArray[parent] < bottom )
{
currArray[currIndex] = currArray[parent];
currIndex = parent;
parent = (parent-1) / 2;
}
currArray[currIndex] = bottom;
}

//siftdown
public void siftDown(int [] currArray , int currIndex)
{
int largerChild;
int top = currArray[currIndex];
while(currIndex < currentSize/2)
{
int leftChild = 2*currIndex+1;
int rightChild = leftChild+1;

if(rightChild < currentSize && currArray[leftChild] < currArray[rightChild] )
largerChild = rightChild;
else
largerChild = leftChild;

if( top >= currArray[largerChild] )
break;

currArray[currIndex] = currArray[largerChild];
currIndex = largerChild;
}
currArray[currIndex] = top;
}

//remove max element
public int removeMaxElement(int [] currArray)
{
int root = currArray[0];
currArray[0] = currArray[--currentSize];
siftDown(currArray , 0);
return root;
}

//heapsort
private void _sortHeapArray(int [] currArray)
{
while(currentSize != 0)
{
removeMaxElement(currArray);
}
}

public void sortHeapArray()
{
_sortHeapArray(currArray);
}

//hepify
private int[] heapify(int[] currArray)
{
int start = (currentSize) / 2;
while (start >= 0)
{
siftDown(currArray, start);
start--;
}
return currArray;
}

//swap
private int[] swap(int[] currArray, int index1, int index2)
{
int swap = currArray[index1];
currArray[index1] = currArray[index2];
currArray[index2] = swap;
return currArray;
}

//heapsort
public int[] _heapSort(int[] currArray)
{
heapify(currArray);
int end = currentSize-1;
while (end > 0)
{
currArray = swap(currArray,0, end);
end--;
siftDown(currArray, end);
}
return currArray;
}
public void heapSort()
{
_heapSort(currArray);

}

//main method
public static void main (String [] args) throws IOException
{
HeapSort mySort = new HeapSort(10);
Employee [] myArray = new Employee[10];

String firstFile = ("Employee.txt");
FileReader file = new FileReader(firstFile);
BufferedReader br = new BufferedReader(file);

String secondFile = ("SortedEmployee.txt");
PrintWriter outputFile = new PrintWriter(secondFile);

String[] currArray = new String[10];
String lineContent;
while ((lineContent = br.readLine()) != null)
{
for (int i = 0; i < currArray.length; i++)
{
lineContent = br.readLine();
currArray[i] = String.valueOf(lineContent);
mySort.heapSort();
outputFile.println(mySort);
}
}
outputFile.close();
System.out.print("Done");

}
}

我知道问题出在 while 循环的 main 方法中,但我只是不知道如何解决它。

员工.txt:

086244

Sally L. Smith

100000.00

Accounting

Manager

7

096586

Meredith T. Grey

150000.00

Physical Therapy

Doctor

9

875236

Christina R. Yang

190000.00

Cardiology

Resident

10

265893

George A. O'Malley

98000.00

Pediatrics

Attending

7
etc......

最佳答案

您尚未实现Comparable<Employee> 。应该是这样的。

public class Employee implements Comparable<Employee>{
@Override
public int compareTo(Employee emp){
return this.empId.compareToIgnoreCase(emp.empId);
}
}

只需使用 Arrays.sort()方法对数组进行排序。

关于java - 通过传递txt文件创建一个数组并将数组引用变量传递给heapsort方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23534040/

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