Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
I want to compare the element of two arrays, here is my code:
Java
public class twoarrs {

		boolean arrays(int[] arr1, int[] arr2){
//			arr1 = new int[2];
//			arr2 = new int[arr1.length];
						
			for (int i = 0; i <= arr1.length - 1; i++){
				for (int j = 0; j <= arr2.length - 1; j++){
					if (arr1[i] < arr2[j]){
						System.out.println("this is true");
					}
					else{
						System.out.println("this is false");	
					}						
				}
			}
                        return true;

		}
		
		public static void main(String[] args){
			twoarrs compare = new twoarrs();
			int[] a1 = {4,5};
			int[] a2 = {1,200};
			compare.arrays(a1, a2);
		}
}


This code will print four results on the screen(for now they are false, true, false, true), how can I return a true value if all the elements in a1 are smaller than the elements in a2? Means I want to return a value after all the for loops are executed.

And why the result will be all false if I add the code which were hidden?

Any help? Thank you.
Posted
Updated 6-Dec-12 6:01am
v2

If I have understood your question correctly
You can Try this Method instead
Java
boolean arrays(int[] arr1, int[] arr2)
{
       for (int i = 0; i <= arr1.length - 1; i++)
	   for (int j = 0; j <= arr2.length - 1; j++)
		if (arr1[i] > arr2[j])
		    return false;								
       return true;
}


About your last question, If you meant commented code instead of hidden code the answer is that
when arrays are constructed the default value is 0 so
arr1 = {0,0}
arr2 = {0,0}
so the results are false
 
Share this answer
 
Comments
CPallini 6-Dec-12 15:31pm    
5.
TorstenH. 7-Dec-12 1:20am    
False!
Means I want to return a value after all the for loops are executed.

You are only checking for the the first higher value. That's not wanted.
Pakh 7-Dec-12 4:13am    
if you find i and j so that arr1[i] > arr2[j]
you know for sure that the result is false
anything you do after that is extra
If you really need to return value after all fors you can define a bool,
and instead of returning assign the value to your bool and at the end return your boolean
unbelievablename 7-Dec-12 8:06am    
But will the loop keep running when it find an arr1[i]>arr2[j]? I mean I only have elements in the arrays here, what if I have hundreds of elements?
Pakh 7-Dec-12 8:41am    
No, in my code it returns false right away when *arr1[i]>arr2[j]* happens.
about the second part I don't understand what you are saying,( your arrays can have hundereds of elements)
Java
private boolean arrays(int[] arr1, int[] arr2){
  boolean bReturn = false; // pretension for return value
  for (int i = 0; i <= arr1.length - 1; i++) {
    for (int j = 0; j <= arr2.length - 1; j++) {
      if (arr1[i] > arr2[j]){
        bReturn = true; // if it happens that arr1 has a higher value than arr2, the return value is changed
      }		
    }
  }
  return bReturn; // done. return result. if "true" arr1 has at least one higher value than the lowest value of arr2.
}
 
Share this answer
 
v3
Comments
unbelievablename 7-Dec-12 8:00am    
I think bReturn will be changed four times if I write like this...but all I want is a bReturn which is only changed once after all the elements are compared.
TorstenH. 7-Dec-12 8:03am    
it's always set to true - so doesn't matter if done multiple times.
unbelievablename 7-Dec-12 8:22am    
Then it is no difference with the original code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900