Click here to Skip to main content
15,884,298 members

How can I get a value after compare all the elements of two arrays?

unbelievablename asked:

Open original thread
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.
Tags: Java

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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