Click here to Skip to main content
13,446,392 members (33,832 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to compare the element of two arrays, here is my code:
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");
						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 6-Dec-12 5:54am
Updated 6-Dec-12 6:01am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If I have understood your question correctly
You can Try this Method instead
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
CPallini 6-Dec-12 15:31pm
TorstenH. 7-Dec-12 1:20am
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)
unbelievablename 7-Dec-12 9:34am
So it doesn't need a "break" in the code right? In the second I mean if I have hundreds elements in the arrays and the loop will keep running even if it find an arr1[i] > arr2[j], the program may have a low efficiency.
Pakh 7-Dec-12 10:51am
No there is no need for break
when you use return it will return the value then it will end the method
(for example if you have two arrays of 100 and i=0,j=0,arr1[i]>arr2[j] then it will return true and the method will end there)

For more details you can look for (in any book about java) break,continue,return,break Label,etc
unbelievablename 7-Dec-12 14:32pm
Okay I see, thank you very much.
Pakh 7-Dec-12 14:48pm
your wellcome
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180314.2 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100