Java 从位于已导入的另一个包中的其他类调用方法
我是一名计算机科学本科生,我们实际上已经开始学习Java语言。 我试图解决我的一个实验室,但我有一个问题。 我的问题是如何从位于另一个包中的另一个类调用方法,并且该包已经导入到我的类中。 我试着写类的名称。方法参数的名称;但这对我不起作用。 更具体地说,我尝试调用位于frame包和SortArray类中的getElementAtindex方法。。但我不知道为什么这对我不起作用 这是我的QuickPortB课程:Java 从位于已导入的另一个包中的其他类调用方法,java,oop,Java,Oop,我是一名计算机科学本科生,我们实际上已经开始学习Java语言。 我试图解决我的一个实验室,但我有一个问题。 我的问题是如何从位于另一个包中的另一个类调用方法,并且该包已经导入到我的类中。 我试着写类的名称。方法参数的名称;但这对我不起作用。 更具体地说,我尝试调用位于frame包和SortArray类中的getElementAtindex方法。。但我不知道为什么这对我不起作用 这是我的QuickPortB课程: package lab; import frame.SortArray; pub
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;
}
}
}
这是我的魔法课:
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;
}
}
您试图将方法getElementAt作为静态函数调用。您必须创建SortArray的实例,然后调用该对象上的方法,例如
ArrayList<String[]> myList = ...; // some initialization
SortArray sortObject = new SortArray(myList);
SortingItem result = sortObject.getElementAt(0);
相比之下:
public static void doSomethingStatic() {
this.numberOfItems++; // this is not allowed
}
要调用方法,必须知道该方法是静态方法类方法还是对象方法 如果它是静态方法,就像著名的mainString[]参数一样,您可以通过ClassName.method调用它,如果它不是静态方法,您必须首先通过调用构造函数获取类的实例,然后通过oneInstance.method调用该方法 我建议你先阅读课堂教材中的相关章节,然后自己做一些测试
我只是不给出代码,因为这是一个赋值。创建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
}
您试图访问函数的方式就像它是静态的一样,因为您试图通过实际的类而不是从对象调用它 您应该生成一个引用所需类的对象,在本例中为SortArray 快速入门
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;
}
}
}
希望对您有所帮助^^这不会编译,因为您试图访问在静态上下文中不可用的成员变量This.listOfItems。
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;
}
}
}