3

I am writing a loop that assigns the number 15 to every element in an array without using any comparison operators such as such as <,==,>, or !=.

There's apparently a way to do this using exception handling.

Any ideas?

Here's what I tried:

public class ArrayProblem {


public static void main(String[] args) {

    int[] arrayElements = {0,0,0,0,0};
    boolean isValid = true;
    System.out.println("Array element values before: " + arrayElements[0] + "," + arrayElements[1] + "," + arrayElements[2] + "," + arrayElements[3] + "," + arrayElements[4]);

   try
    {
       while(isValid)
       {
       throw new Exception();
       }
     }

   catch(Exception e)
    {
        System.out.println(e.getMessage());
    }

    finally
    {
        //finally block executes and assigns 15 to each array element
        arrayElements[0] = 15;
        arrayElements[1] = 15;
        arrayElements[2] = 15;
        arrayElements[3] = 15;
        arrayElements[4] = 15;
        System.out.println("New array element values are " + arrayElements[0] + "," + arrayElements[1] + "," + arrayElements[2] + "," + arrayElements[3] + "," + arrayElements[4]); 
    }
  }
}
3
  • 2
    1.Why introducing try ... catch when you can do without Commented Jun 21, 2015 at 1:54
  • Your solution assumes that the size of the array is 5. Is that the problem? You need to do it dynamically? Commented Jun 21, 2015 at 2:02
  • The size of the array is arbitrary, I just used 5 array elements for my example, it could be any number of elements. Commented Jun 21, 2015 at 2:06

2 Answers 2

5

Arrays.fill(intArray, 15);

Internally, this function likely does comparisons, but maybe it meets your constraints?

If the solution requires a loop, here is another way with no direct comparisons:

int[] array = new int[10];
int arrIdx = -1;
for (int i : array){
    arrIdx++;
    array[arrIdx]=15;
    System.out.println(array[arrIdx]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

That may just work, I think the only constraint in the problem is that the loop has to assign the value 15 to each element of the array with using the stated operators, so I can't see why the Arrays.fill function couldn't be used to solve the problem.
@SteveWalsh Just added another solution which does not require try-catch.
3

This is a terrible idea in real code. If you simply need a hackish solution, just loop while incrementing an index counter, accessing each array element, until you go out of bounds

// prepare array, arbitrary size
Random random = new Random();
int size = random.nextInt(20);
int[] array = new int[size];

int i = 0;
// solution
try {
    for (;;)
        array[i++] = 15;
} catch (ArrayIndexOutOfBoundsException e) {
    // ignore
}

// verify
System.out.println(Arrays.toString(array));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.