Java 从数组中删除项
刚开始学习Java2,自从我学习Java1并做了很多编程以来,已经有6-7个月了,所以如果我做了一些愚蠢的事情,请原谅我 下面三个类中的大多数用于上一个赋值,您从书中获取了一个列表,并且必须添加一个名为getMax()的方法,该方法返回数组中最高键的值,如果数组为空,则返回-1 这部分很好 对于下一部分(我遇到问题的部分),我们必须修改赋值,以便具有最高键的项不仅由方法返回,而且从数组中删除 为此,我尝试:Java 从数组中删除项,java,arrays,methods,Java,Arrays,Methods,刚开始学习Java2,自从我学习Java1并做了很多编程以来,已经有6-7个月了,所以如果我做了一些愚蠢的事情,请原谅我 下面三个类中的大多数用于上一个赋值,您从书中获取了一个列表,并且必须添加一个名为getMax()的方法,该方法返回数组中最高键的值,如果数组为空,则返回-1 这部分很好 对于下一部分(我遇到问题的部分),我们必须修改赋值,以便具有最高键的项不仅由方法返回,而且从数组中删除 为此,我尝试: public long removeMax(PrintWriter pw) {
public long removeMax(PrintWriter pw)
{
long maxIndex;
maxIndex = getMax();
delete(maxIndex);
return maxIndex;
}
但是maxIndex=getMax();删除(maxIndex);给了我错误,我不太清楚为什么。虽然我确信我只是犯了一个小错误,因为我对编程很生疏
它给我的错误是,实际参数列表和形式参数列表的长度不同。我试着改变和改变一些事情,但不管我做什么,似乎都不起作用
下面是完整的Employee、HighArrayObject和Project21Rev(getMax和removeMax所在的类)类
public class Employee
{
protected int empNo; // employee number
protected String ssno; // social security number
protected String lastName; // Last name
protected String firstName; // First name
//This constructor initializes the variables
public Employee(int eNo, String ssn, String lName, String fName)
// aliases are used in the function header
{
empNo = eNo; // the alias is assigned to the actual value
ssno = ssn;
lastName = lName;
firstName = fName;
}
// Make a no argument constructor as well
public Employee()
{
empNo = 0;
ssno = "";
lastName = "";
firstName = "";
}
/**
The copy constructor initializes the object
as a copy of another Employee object
@param object2 The object to copy
*/
public Employee(Employee object2)
{
empNo = object2.empNo;
ssno = object2.ssno;
lastName = object2.lastName;
firstName = object2.firstName;
}
// The set method sets a value for each field
public void set(int eNo, String ssn, String lName, String fName)
// aliases are used in the function header
{
empNo = eNo; // the alias is assigned to the actual value
ssno = ssn;
lastName = lName;
firstName = fName;
}
// the getKey method returns the employee number
public int getKey()
{ return empNo; }
// the setKey method sets the employee number
public void setKey(int id)
{ empNo = id; }
// toString method
// returns a string containing the instructor information
public String toString()
{
// Create a string representing the object.
String str = "Employee Number: " + empNo +
"\nSocial Security Number: " + ssno +
"\nLast Name: " + lastName +
"\nFirst Name: " + firstName;
// Return the string;
return str;
}
}
下节课开始
import java.io.*;
class HighArrayObject
{
protected Employee[] emp;
protected int nElems;
public HighArrayObject(int max) // constructor
{
emp = new Employee[max];
nElems = 0;
}
// The createEmployees method creates an Employee object
// for each element of the array
public static void createEmployees(Employee[] emp, int maxsize)
{
int empNo;
String ssno;
String lastName;
String firstName;
// Create the employees
for(int index = 0; index < emp.length; index++)
{
// Get the employee data
emp[index] = new Employee();
}
}
public boolean find(long searchKey, PrintWriter pw)
{
System.out.println("Trying to find item with employee number " + searchKey);
pw.println("Trying to find item with employee number " + searchKey);
int j;
for(j=0; j<nElems; j++)
if(emp[j].empNo == searchKey) // == ok since empNo is a primative
break; // exit loop before end
if(j == nElems) // gone to end?
return false;
else
return true; // no, found it
} // end find()
public void insert(int eNo, String sNo, String lName, String fName, PrintWriter pw)
{
System.out.println("Inserting employee with employee number " + eNo);
pw.println("Inserting employee with employee number " + eNo);
Employee temp = new Employee();
temp.empNo = eNo;
temp.ssno = sNo;
temp.lastName = lName;
temp.firstName = fName;
emp[nElems] = temp;
nElems++;
}
public boolean delete(long value, PrintWriter pw)
{
System.out.println("Deleting employee number " + value);
pw.println("Deleting employee number " + value);
int j;
for(j=0; j < nElems; j++) // look for it
if(value == emp[j].empNo)
break; // can't find it
if(j==nElems)
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move higher ones down
{
emp[k]=emp[k+1];
}
nElems--; // decrement size
return true;
}
} // end delete
public void display(PrintWriter pw)
{
System.out.println("The array of employees is: ");
pw.println("The array of employees is: ");
for(int j=0; j<nElems; j++)
{
System.out.println(emp[j].empNo + " " + emp[j].ssno + " "
+ emp[j].lastName + " " + emp[j].firstName);
pw.println(emp[j].empNo + " " + emp[j].ssno + " "
+ emp[j].lastName + " " + emp[j].firstName);
} // end for
} // end delete
} // end HighArrayObject
import java.io.*;
public class Project21Rev extends HighArrayObject //reference Gaddis p.658
{
public Project21Rev(int max) // subclass constructor
{
super(max); // call superclass constructor
}
public void getMax(PrintWriter pw) // new functionality as required by the assignment
{
int maxIndex = -1; // not found yet
if(nElems == 0)
System.out.println("Number of elements is 0");
else
{
int max = emp[0].empNo; // assume the first value is the largest
maxIndex = 0;
for (int i = 1; i < nElems; i++) //now check the rest of the values for largest
{
if(emp[i].empNo > max)
{
maxIndex = i;
}
}
System.out.println("The largest value is " + emp[maxIndex].empNo + " " + emp[maxIndex].ssno + " " + emp[maxIndex].lastName + " " + emp[maxIndex].firstName);
pw.println("The largest value is " + emp[maxIndex].empNo + " " + emp[maxIndex].ssno + " " + emp[maxIndex].lastName + " " + emp[maxIndex].firstName);
System.out.println("at location " + maxIndex);
pw.println("at location " + maxIndex);
}
}
public long removeMax(PrintWriter pw)
{
long maxIndex;
maxIndex = getMax();
delete(maxIndex);
return maxIndex;
}
// modified find method follows
public void find( int searchKey, PrintWriter pw)
{
System.out.println("Trying to find item with employee number " + searchKey);
pw.println("Trying to find item with employee number " + searchKey);
int j;
Boolean found = false;
for(j=0; j < nElems; j++)
if(emp[j].empNo == searchKey)
{
found = true;
System.out.println("Found " + emp[j].empNo + " " + emp[j].ssno + " " + emp[j].lastName + " " + emp[j].firstName);
pw.println("Found " + emp[j].empNo + " " + emp[j].ssno + " " + emp[j].lastName + " " + emp[j].firstName);
System.out.println("at location " + j);
pw.println("at location " + j);
}
if(found == false)
{
System.out.println(searchKey + " Not found");
pw.println(searchKey + " Not found");
}
}
}
class Project21RevApp
{
public static void main(String[] args) throws IOException
{
// set up printer output file
PrintWriter pw = new PrintWriter(new BufferedWriter
(new FileWriter("output21.dat")));
int maxSize = 100; // array size
Project21Rev arr; // reference to array
arr = new Project21Rev(maxSize); // create the array
arr.insert(77,"A","B","C",pw);
arr.insert(99,"D","E","F",pw);
arr.insert(44,"G","H","I",pw);
arr.insert(55,"J","K","L",pw);
arr.insert(22,"M","N","O",pw);
arr.insert(88,"P","Q","R",pw);
arr.insert(11,"S","T","U",pw);
arr.insert(00,"V","W","X",pw);
arr.insert(66,"Y","Z","AA",pw);
arr.insert(33,"BB","CC","DD",pw);
arr.display(pw); // display items
int searchKey = 35; // search for item
arr.find(searchKey, pw);
searchKey = 22; // search for item
arr.find(searchKey, pw);
arr.delete(00, pw); // delete 3 items
arr.delete(55, pw);
arr.delete(99, pw);
arr.display(pw); // display items again
// new functionality follows
arr.getMax(pw);
pw.close();
} // end main()
} // end class Project21RevApp
import java.io.*;
类HighArrayObject
{
受保护雇员[]环境管理计划;
受保护的内环;
public HighArrayObject(int max)//构造函数
{
emp=新员工[最大值];
nElems=0;
}
//createEmployees方法创建雇员对象
//对于数组的每个元素
公共静态void createEmployees(雇员[]emp,int-maxsize)
{
int empNo;
字符串ssno;
字符串lastName;
字符串名;
//创造员工
对于(int index=0;index 对于(j=0;j嗯,错误消息实际上会准确地告诉您出了什么问题
您的方法delete()
接受两个参数:
public boolean delete(long value, PrintWriter pw) {
// ...
}
您正在尝试仅使用一个来调用它:
delete(maxIndex);
若要修复错误,请传递正确数量和类型的参数。Hymm错误太多。请从了解a是什么以及如何调用它开始。您正在尝试调用您的方法
public void getMax(PrintWriter pw){..}
与
现在您可以看到,当您取消定义该方法时,您使用VOID告诉它应该不返回任何内容。但它需要一个PrintWriter对象。您在创建以下对象时的想法是正确的:
public long removeMax(PrintWriter pw)
您可以看到该方法应该接收这样的变量/对象,它将是“pw”。
因此,调用方法的正确方法是
getMax(pw);
如果你想让它返回一些东西,你真的应该多读一些关于java及其工作方式的书
另一个大问题是,您正在谈论一个,但是在代码中我看到了
protected Employee[] emp;
Witch根本不是一个数组,只是一个简单的数组。这是一个很大的区别。如果要求你有一个实际的java/编程列表,那么程序中就没有这样的东西。它只是一个数组,看起来像一个一般的列表,但在大多数编程语言中被称为数组。也许你的老师没有区别。
这不是一个完整的解决方案,但是有太多的事情需要修复,代码太大,不适合它所要做的任何事情。不要感到压力,只要继续阅读来自任何来源的Java。请记住,您使用的是数组,而不是代码中的列表,您需要学习有关该语言的一些基本信息
protected Employee[] emp;