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!!

For debugging, You should also print out the values of miniNumber, i and number so you know what the if statement is seeing.

What is wrong with what is printed out? What is the correct print out?

Hello NormR1.

Thank you for responding.

The getNumber() method is supposed to select the smallest array contained within the Object array. The small array, which is assigned to number, feeds into another method for finding the intersection between the query array and the other simple arrays contained in the Object array.

Now, I have adjusted the code to show what miniNumber and Size are doing. It does select the smallest array in the collection, but the logic seems faulty. By the way, I have played around with the "if" statement combinations, to no avail.

    public static int getNumber(Object[] collections)
        {
            int number = 0;
            int count = 0;
            int size = 0;

            /*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 + "size"); //Identify size variable.
                System.out.println(miniNumber + "miniNumber");  //Identify miniNumber variable.

            /*Here is where I am trying to make the comparison.*/

                    if(miniNumber > size)
                    {
                        number = i;
                        System.out.println("Query is array " + number);
                    }

            }

            return number;  //Number is supposed to contain one array.
        }

Here is the driver.

    public static void main(String[] args)
        {
            Integer[] collection1 = {1, 2, 3, 4};
            Integer[] collection2 = {1, 2, 3, 4, 5, 6, 7};
            Integer[] collection3 = {1, 2, 3, 4, 5};
            Integer[] collection4 = {1, 2, 3, 4, 5, 6};
            Object[] storeAllArray = {collection2, collection1, collection3};


            System.out.println(CommonElements.findCommonElements(storeAllArray));   

            System.out.println("Comparisons:  " + CommonElements.getComparisons());

        }

Here is the output.

6size
6miniNumber
3size
6miniNumber
Query is array 1
4size
3miniNumber

Do I need another for loop or do I have to increment miniNumber? Any suggestions? As stated before, I have tried several combinations.

Thanks for taking the time to help.

Sharon

I still don't understand what the problem is with what is printed?
What is the correct output for the program?

Can you make a small, complete program that compiles, executes and shows the problems. The small pieces of code you posted don't compile.

To find the algorithm, write a small simple program with an array of ints. Use a loop to find the index of the smallest element in the array. That will be a simpler program to work on and to post here for help getting the logic to work. When you get that working you can take the technique and copy the logic to the above program.

Edited 4 Years Ago by NormR1

In your first code, take the code line 20 out and place it in line 24...

Your method returns a correct number, but you are displaying a minimum number every time it gets change. In other words, the display doesn't affect the return value.

Edited 4 Years Ago by Taywin

Thanks Taymin. Your suggestion worked. The method is functioning properly.

Thank you, NormR1. Your suggestion to insert print statements to see what size and miniNumber were doing helped verify that the code was working.

    public static int getNumber(Object[] collections)
        {
            int number = 0;
            int count = 0;
            int size = 0;

            /*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 + "size");
                System.out.println(miniNumber + "miniNumber");

            /*Here is where I am trying to make the comparison.*/


                    if(miniNumber > size)
                    {
                        number = size;
                    }

            }

            System.out.println("Query is array " + number);
            return number;  //Number is supposed to contain one array.
        }

Here is the driver:

    public class TestCommonElements 
    {

        public static void main(String[] args)
        {
            Integer[] collection1 = {1, 2, 3, 4};
            Integer[] collection2 = {1, 2, 3, 4, 5};
            Integer[] collection3 = {1, 2, 3, 4, 6, 7};
            Integer[] collection4 = {1, 2, 3, 4, 6};
            Object[] storeAllArray = {collection4, collection3, collection2, collection1};


            System.out.println(CommonElements.findCommonElements(storeAllArray));   

            System.out.println("Comparisons:  " + CommonElements.getComparisons());

        }
    }

Here is the output:

4size
4miniNumber
5size
4miniNumber
4size
4miniNumber
3size
4miniNumber
Query is array 3

One "problem" with the code is the names that are used. If the name of the method was: getIndexOfLowest()
and the variable: number was changed to indexOfLowest the logic would be clearer.

You have changed the code. Does the code you posted work correctly? Have you tested it with all possible inputs? For example try it with a set of arrays with all their lengths greater than the number of arrays.

Edited 4 Years Ago by NormR1

This article has been dead for over six months. Start a new discussion instead.