I need to write a method that returns true if all the values in two int arrays are in the range 0-10, otherwise the method returns false. The code I have written is below but is there a better/more effient way to do this?

private int[] myArray1;
private int[] myArray2;

private boolean validNumbers()
{
        for(int i = 0; i < myArray1; i++)
        {
              if(myArray1[i] < 0 || myArray1[i] > 9)
                     return false;
        }
 
        for(int i = 0; i < myArray2; i++) 
        {
               if(myArray2[i] < 0 || myArray2[i] > 9)
                     return false;
        }

        return true;
}

Thanks

Ok so I wrote a method so that you don't have to duplicate your code for each array all you have to do is pass the array to the method and it will return true if all the numbers are less than 10.

I also made the method static so that you could just call it using the name of it's class if you are calling the method from another class, but if you don't need that than just take off the static keyword from the method declaration.

public static boolean Arraycheck(int[] ArrayToCheck){
    boolean TheArray = true;
    for(int count = 0; count < ArrayToCheck.length; ++count){
        if(ArrayToCheck[count] > 10){
            TheArray = false;
            break;
        }
    }
    return TheArray;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.