📄 mljarray.java
字号:
if (((Boolean) sortMethod.invoke (elements[Down],args)) .booleanValue ())
break;
}
if (Up < Down)
{
temp = elements[Up];
elements[Up] = elements[Down];
elements[Down] = temp;
}
}
temp = elements[minpos];
elements[minpos] = elements[Down];
elements[Down] = temp;
qsort(elements,minpos,Down - 1,sortMethod);
qsort(elements,Down + 1, maxpos,sortMethod);
} catch(Exception e)
{
e.printStackTrace();
}
}
/** Finds the maximum value in an array of double values.
* @return The maximum value.
* @param index An IntRef holding the place of the maximum value in the
* array.
* @param array The array to be searched.
*/
static public double max(IntRef index, double[] array)
{
if (array.length == 0)
Error.fatalErr("Array<Element>::max() - empty array");
double max = array[0];
index.value = 0;
for(int i = 1 ; i < array.length ; i++)
if (array[i] > max)
{
max = array[i];
index.value = i;
}
return max;
}
/** Searches an array of double values for a specified value.
* @return The first index of the value searched for if found, otherwise -1.
* @param key The value to be searched for.
* @param startPosition The array index to start searching from.
* @param array The array to be searched.
*/
static public int find(double key, int startPosition, double[] array)
{
if (startPosition < 0 || startPosition > array.length)
Error.fatalErr("Array<>::find: start position " +startPosition+ " is out "
+ "of the legal range [0-" +array.length+ "]");
for(int idx = startPosition ; idx < array.length ; idx++)
if (array[idx] == key)
return idx;
// could not find it--return -1
return -1;
}
/** Sorts the specified array of integers.
* @param theArray The array to be sorted. The array is changed by the use
* of this method.
*/
static public void sort(int[] theArray)
{
qsort(theArray,0,theArray.length-1);
}
/***************************************************************************
Recursive Quicksort of an array of integers.
@param elements The elements to be sorted in this iteration.
@param minpos The minimum position included in this iteration.
@param maxpos The maximum position included in this iteration.
@deprecated
***************************************************************************/
static private void qsort(int[] elements, int minpos, int maxpos)
{
if (minpos >= maxpos)
return;
int Up = 0;
int Down = 1;
int temp = elements[Up];
while(Up < Down)
{
for(Up = minpos + 1 ; Up <= maxpos ; Up++)
if (elements[Up] > elements[minpos])
break;
for(Down = maxpos ; Down >= minpos ; Down--)
if (elements[Down] <= elements[minpos])
break;
if (Up < Down)
{
temp = elements[Up];
elements[Up] = elements[Down];
elements[Down] = temp;
}
}
temp = elements[minpos];
elements[minpos] = elements[Down];
elements[Down] = temp;
qsort(elements,minpos,Down - 1);
qsort(elements,Down + 1, maxpos);
}
/** Counts the number of values in a boolean array match a specified value.
* @return The number of elements which match the specified value.
* @param key The value to be searched for.
* @param array The array to be searched.
*/
static public int num_element(boolean key, boolean[] array)
{
int sum = 0;
for(int idx = 0 ; idx < array.length ; idx++)
if (array[idx] == key)
sum++;
return sum;
}
/** Copies the given integer array and returns the copy.
* @param source The array to be copied.
* @return The new copy.
*/
static public int[] copy(int[] source)
{
int[] rtrn =new int[source.length];
for(int i = 0;i < source.length;i++)
rtrn[i] = source[i];
return rtrn;
}
/** Copies the given double array and returns the copy.
* @param source The array to be copied.
* @return The new copy.
*/
static public double[] copy(double[] source)
{
double[] rtrn =new double[source.length];
for(int i = 0;i < source.length;i++)
rtrn[i] = source[i];
return rtrn;
}
/** Copies the given boolean array and returns the copy.
* @param source The array to be copied.
* @return The new copy.
*/
static public boolean[] copy(boolean[] source)
{
boolean[] rtrn =new boolean[source.length];
for(int i = 0;i < source.length;i++)
rtrn[i] = source[i];
return rtrn;
}
/*class Array {
NO_COPY_CTOR(Array);
private boolean owner;
protected int base;
protected int arraySize;
protected Object[] elements;
protected void alloc_array(int lowVal, int sizeVal)
{
if (sizeVal < 0)
Error.fatalErr("Array<>::Illegal bounds requested, base: "+lowVal+
" high: "+(lowVal + sizeVal));
base = lowVal;
arraySize = sizeVal;
elements = new Object[arraySize > 0 ? arraySize : 1];
owner = true;
}
//Shallow copy - JL
protected void copy(Array source)
{
alloc_array(source.base, source.arraySize);
for (int i = 0; i < arraySize; i++)
elements[i] = source.elements[i];
}
#ifdef PC_MSVC_AMBIG
protected Bool equal_given_that_bounds_are_equal( Array<Element>& rhs) ;
#endif
public Array(Array source,Object CtorDummy)
{
copy(source);
}
public Array(int baseStart, int size)
{
alloc_array(baseStart, size);
}
public Array(int size)
{
alloc_array(0, size);
}
public Array(int baseStart, int size,
Object initialValue)
{
alloc_array(baseStart, size);
init_values(initialValue);
}
public Array(Object[] carray, int length)
{
owner = FALSE;
base = 0;
arraySize = length;
elements = carray;
MLJ.ASSERT(length >= 0,"MLJArray.Array: length < 0.");
MLJ.ASSERT(carray != null,"MLJArray.Array: carray == null.");
}
public void truncate(int size)
{
MLJ.ASSERT(size >= 0,"MLJArray.truncate: size < 0.");
arraySize = size;
}
protected finalize() { if (owner) elements = null; }
public void assign(Array elem)
{
if (this != &elem) {
//DBG(if (elem.size() != size())
Error.fatalErr("Array.assign: Cannot assign array sized: "+
elem.size()+" to an array sized: " + size()));
//DBG(if (elem.low() != low())
Error.fatalErr("Array.assign: Cannot assign array starting at: " +
elem.low()+" to an array starting at: "+low());
for (int i = 0; i < arraySize; i++)
elements[i] = elem.elements[i];
}
return this;
}
public Object[] get_elements() { return elements; }
public void init_values(Object initialValue)
{
// walk through the array and initialize all of the values
for (int i = 0; i < arraySize; i++)
elements[i] = initialValue;
}
public int high(){ return base + arraySize - 1; }
public int low(){ return base; }
public int size(){ return arraySize; }
// read, write, display need MLCstream routines for reading/writing
// the object.
public void read_bin(MLCIStream& str);
public void write_bin(MLCOStream& str);
public void display(MLCOStream& stream = Mcout) ;
public Array<Element>& operator +=( Array<Element>& item);
public Array<Element>& operator -=( Array<Element>& item);
// CFront 3 chokes if these are moved outside the class
// and defined as inline (it generates static function undefined).
public Object index(int i) {
DBG(if (i < 0 || i >= arraySize)
err << "Array<>::index: reference " << i
<< " is out of bounds (0.." << arraySize - 1
<< ')' << fatal_error
);
return elements[i];
}
public Object index(int i) {
DBG(if (i < 0 || i >= arraySize)
err << "Array<>::index() : reference " << i
<< " is out of bounds (0.."
<< arraySize - 1 << ')' << fatal_error
);
return elements[i];
}
public Object operator[](int i) {
DBG(if (i < base || i > base + arraySize - 1)
err << "Array<>::operator[]: reference " << i
<< " is out of bounds (" << base
<< ".." << high() << ')' << fatal_error
);
return elements[i - base];
}
public Object operator[](int i) {
DBG(if (i < base || i > base + arraySize - 1)
err << "Array<>::operator[] : reference " << i
<< " is out of bounds (" << base << ".." << high()
<< ')' << fatal_error
);
return elements[i - base];
}
// The following two methods return the same data as the index()
// methods--but the fast_index() methods do NOT do ANY error checking.
// They are provided for speed--not safety.
public Object fast_index(int i) {return elements[i];}
public Object fast_index(int i) {return elements[i];}
// sort, min/max need operator<
public void sort();
public Object max(int& idx) ; // idx is zero based index to array
public Object max() ;
public Object min(int& idx) ; // idx is zero based index to array
public Object min() ;
public min_max( Object min, Object max) ;
public Element sum() ; // Sum the elements of the array.
public int find( Object key, int startPos=0) ;
public int num_element( Object key) ;
public MString get_element_indexes( Object key) ;
#ifdef PC_MSVC_AMBIG
public Bool bounds_are_equal( Array<Element>& rhs) ;
public Bool operator==( Array<Element>& rhs) ;
public Bool operator!=( Array<Element>& rhs) ;
#endif
};
*/
/** This function truncates an array of Objects.
* @param newSize The new size of the array.
* @param array The array to be truncated.
* @return The truncated array.
*/
static public Object[] truncate(int newSize, Object[] array)
{
MLJ.ASSERT(array.length >= 0, "Array::truncate: array.length < 0");
Object[] newArray = (Object[])Array.newInstance(array.getClass().getComponentType(),newSize);
for(int i = 0 ; i < newArray.length ; i++)
newArray[i] = array[i];
return newArray;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -