我是本科计算机科学专业的学生,我们实际上已经开始学习Java语言了。 我正在尝试解决我的一个实验室,但我遇到了问题。 我的问题是如何从另一个类中调用一个方法,该类位于另一个包中,并且该包已经在我的类中导入。 我试着写下nameOftheClass.nameOfthemethod(参数);但这对我没有用。 为了更加Specefic,我试图调用方法getElementAt(index),该方法位于frame包和SortArray类中。但我不知道为什么这对我不起作用!
这是我的QuicksortB类:
package lab;
import frame.SortArray;
public class QuickSortB extends QuickSort {
/**
* Quicksort algorithm implementation to sort a SorrtArray by choosing the
* pivot as the median of the elements at positions (left,middle,right)
*
* @param records
* - list of elements to be sorted as a SortArray
* @param left
* - the index of the left bound for the algorithm
* @param right
* - the index of the right bound for the algorithm
* @return Returns the sorted list as SortArray
*/
@Override
public void Quicksort(SortArray records, int left, int right) {
// TODO
// implement the Quicksort B algorithm to sort the records
// (choose the pivot as the median value of the elements at position
// (left (first),middle,right(last)))
int i = left, j = right;
//Get The Element from the Middle of The List
int pivot = SortArray.getElementAt(left + (right-left)/2);
//Divide into two Lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (SortArray.getElementAt(i) < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (SortArray.getElementAt(j) > pivot) {
j--;
}
// If we have found a values in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
exchange(i,j)
i++;
j--;
}
}
public void exchange(int i, int j) {
int temp = SortArray.getElementAt(i);
SortArray.getElementAt(i) = SortArray.getElementAt(j);
SortArraz.getElementAt(j) = temp;
}
}
}
这是我的SortArray类:
package frame;
import java.util.ArrayList;
import lab.SortingItem;
/**
* Do NOT change anything in this class!
*
* The SortArray class provides simple basic functions, to store a list of
* sortingItems to track the number of operations.
*
* This class contains two members (readingOperations and writingOperations)
* that act as counters for the number of accesses to the arrays to be sorted.
* These are used by the JUnit tests to construct the output. The methods
* provided in this class should be sufficient for you to sort the records of
* the input files.
*
* @author Stefan Kropp
*/
public class SortArray {
private int numberOfItems;
private ArrayList<SortingItem> listOfItems;
private int readingOperations;
private int writingOperations;
/**
* @param numberOfItems
* number of items to hold
*/
public SortArray(ArrayList<String[]> items) {
numberOfItems = items.size();
readingOperations = 0;
writingOperations = 0;
listOfItems = new ArrayList<>();
for (String[] element : items) {
SortingItem s = new SortingItem();
s.BookSerialNumber = element[0];
s.ReaderID = element[1];
s.Status = element[2];
listOfItems.add(s);
}
}
/**
* sets the elements at index. if index is >= numberOfItems or less then
* zero an IndexOutOfBoundException will occur.
*
* @param index
* the index of the Elements to set
* @param record
* a 3-dimensional record which holds: BookSerialNumber,
* ReaderID, Status
*/
public void setElementAt(int index, SortingItem record) {
this.listOfItems.set(index, record);
writingOperations++;
}
/**
* Retrieves the information stored at position Index. if index is >=
* numberOfItems or less then zero an IndexOutOfBoundException will occur.
*
* @param index
* Index defines which elements to retrieve from the SortArray
* @return Returns a 3-dimensional String array with following format:
* BookSerialNumber, ReaderID, Status.
*
*/
public SortingItem getElementAt(int index) {
SortingItem result = new SortingItem(this.listOfItems.get(index));
readingOperations++;
return result;
}
/**
* @return Returns the number of reading operations.
*/
public int getReadingOperations() {
return readingOperations;
}
/**
* @return Returns the number of writing operations.
*/
public int getWritingOperations() {
return writingOperations;
}
/**
* @return Returns the numberOfItems.
*/
public int getNumberOfItems() {
return numberOfItems;
}
}
答案 0 :(得分:0)
您试图将方法getElementAt
称为静态函数。您必须创建SortArray的实例,然后在该对象上调用该方法,例如
ArrayList<String[]> myList = ...; // some initialization
SortArray sortObject = new SortArray(myList);
SortingItem result = sortObject.getElementAt(0);
如果您希望在尝试时可以访问您的功能,则必须使用static
修饰符,这反过来意味着您无权访问该类的成员(即非静态字段)
public void doSomething() {
this.numberOfItems++; // this is allowed
}
与之相反:
public static void doSomethingStatic() {
this.numberOfItems++; // this is not allowed
}
答案 1 :(得分:0)
调用方法,你必须知道方法是静态方法(类方法)还是对象方法。
如果它是静态方法,(就像着名的main(String[] args)
)你可以通过ClassName.method()
来调用它,如果它不是静态方法,你必须首先得到类的实例,例如,调用构造函数,然后通过oneInstance.method()
我建议你阅读讲座课本中的相关章节,然后自己做一些测试。
我只是不给代码,因为这是一项任务。
答案 2 :(得分:0)
创建SortArray对象的实例并使用该引用调用以检查以下代码是否清晰
public class QuickSortB extends QuickSort {
//create instance of sortArray
SortArray sortArray=new SortArray();
//call the method like this
sortArray.getElementAt(i)
}
public class SortArray {
// code skipped for clarity
public SortingItem getElementAt(int index) {
SortingItem result = new SortingItem(this.listOfItems.get(index));
readingOperations++;
return result;
}
// code skipped for clarity
}
答案 3 :(得分:0)
您尝试访问该函数的方式就像它是静态的一样,因为您尝试通过实际的类调用它,而不是从对象调用它。
您应该生成一个引用所需类的对象,在本例中为SortArray。
在QuickSortB中
public SortArray sortArray;
@Override
public void Quicksort(SortArray records, int left, int right) {
// TODO
// implement the Quicksort B algorithm to sort the records
// (choose the pivot as the median value of the elements at position
// (left (first),middle,right(last)))
sortArray = new SortArray ();
int i = left, j = right;
//Get The Element from the Middle of The List
int pivot = sortArray.getElementAt(left + (right-left)/2);
//Divide into two Lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (sortArray.getElementAt(i) < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (sortArray.getElementAt(j) > pivot) {
j--;
}
// If we have found a values in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
exchange(i,j)
i++;
j--;
}
}
public void exchange(int i, int j) {
int temp = SortArray.getElementAt(i);
sortArray.getElementAt(i) = SortArray.getElementAt(j);
sortArray.getElementAt(j) = temp;
}
}
}
希望它有所帮助^^