public class SortedListArray {
/*
* This class implements the sorted list in java using the array data structure.
*/
private final int DEFCAP = 20; // default capacity of the list
private T[] elements; // array to store the elements
private int numElements;
// some variables for internal use: these will be used by the contains and remove method
private boolean isFound;
private int location;
// constructors
public SortedListArray() {
elements = (T[]) new Object[DEFCAP];
numElements = 0;
}
public SortedListArray(int CUSTOMCAP) {
elements = (T[]) new Object[CUSTOMCAP];
numElements = 0;
}
// helper functions
public int size() {
return numElements;
}
public boolean isFull() {
return (numElements == elements.length);
}
public boolean isEmpty() {
return (numElements == 0);
}
private void find(T element) {
/*
* This method loops through the list and checks if the provided element is present.
* To optimize the searching process, it implements a binary search algorithm
* Over here, we implement the algorthim in an iterative way, reason being, the number of elements in the array is going to be large, around 500-1000, for which, an iterative approach will be more efficient.
*/
// resetting the isFound and location variables from previous runs
isFound = false;
location = 0;
int mid; // storing the middle index of the array
int low = 0;
int high = elements.length-1;
// lopping through
while (low
Подробнее здесь: [url]https://stackoverflow.com/questions/79218502/java-lang-classcastexception-class-ljava-lang-object-cannot-be-cast-to-class[/url]
Я определил общий класс, который реализует отсортированный список с использованием массива. Вот класс: [code]public class SortedListArray { /* * This class implements the sorted list in java using the array data structure. */
private final int DEFCAP = 20; // default capacity of the list private T[] elements; // array to store the elements private int numElements;
// some variables for internal use: these will be used by the contains and remove method private boolean isFound; private int location;
// constructors public SortedListArray() { elements = (T[]) new Object[DEFCAP]; numElements = 0; }
public SortedListArray(int CUSTOMCAP) { elements = (T[]) new Object[CUSTOMCAP]; numElements = 0; }
// helper functions public int size() { return numElements; }
public boolean isFull() { return (numElements == elements.length); }
public boolean isEmpty() { return (numElements == 0); }
private void find(T element) { /* * This method loops through the list and checks if the provided element is present. * To optimize the searching process, it implements a binary search algorithm * Over here, we implement the algorthim in an iterative way, reason being, the number of elements in the array is going to be large, around 500-1000, for which, an iterative approach will be more efficient. */ // resetting the isFound and location variables from previous runs isFound = false; location = 0;
int mid; // storing the middle index of the array int low = 0; int high = elements.length-1;