I have an interesting problem. I am working on a method that iterates through a number of simple java arrays stored in an Object array. The goal of this method is to find the smallest array contained within the Object array. Because these collections are stored in an Object array, I can compare them only by their lengths. Here is what I have so far.
public static int getNumber(Object[] collections)
{
int number = 0; //Variable declaration
int count = 0; //Variable declaration
int size = 0; //Variable declaration
/*locate smallest array for query.*/
for(int i = 0; i < collections.length; i++)
{
Arrays.sort((Comparable[])collections[i]);
size = ((Comparable[])collections[i]).length - 1;
int miniNumber = ((Comparable[]) collections[number]).length - 1;
System.out.println(size);
/*Here is where I am trying to make the comparison.*/
if(miniNumber > size)
{
number = i;
System.out.println("Query is array " + number);
}
}
return number;
}
If my driver program contains arrays of the same length or two arrays that are of the same length and one array that is smaller, the code will work. However, it does not work when the arrays are in descending order.
For example:
public static void main(String[] args)
{
Integer[] collection1 = {1, 2, 3, 4, 5};
Integer[] collection2 = {1, 2, 3, 4, 5, 6};
Integer[] collection3 = {1, 2, 3, 4, 5, 6, 7};
Integer[] collection4 = {1, 2, 3, 4, 5, 6};
Object[] storeAllArray[] = {collection3, collection2, collection1};
The output is
6
5
Query is array # 1
4
Query is array # 2
My goal is to get one query array, but not two. Where am I going wrong?
Thanks!!