比较两个数组的所有元素后,如何获取值? [英] How can I get a value after compare all the elements of two arrays?

查看:79
本文介绍了比较两个数组的所有元素后,如何获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个数组的元素,这是我的代码:

  public  < span class =code-keyword> 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( 这是真的);
}
else {
System.out.println( 这是假);
}
}
}
返回 true;

}

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





此代码将在屏幕上打印四个结果(现在它们是假的,真的,false,true),如果a1中的所有元素都小于a2中的元素,我该如何返回真值?意味着我想在执行所有for循环后返回一个值。



如果我添加隐藏的代码,为什么结果都是假的?



任何帮助?谢谢。

解决方案

如果我已正确理解你的问题

你可以试试这个方法

< pre lang =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;
返回 true;
}





关于你的上一个问题,如果你的意思是注释代码而不是隐藏代码,答案就是

构造数组时默认值为0所以

arr1 = {0,0}

arr2 = {0,0}

所以结果是假的


  private   boolean 数组( int  [] arr1, int  [] arr2){
boolean bReturn = false; // 返回值的预张贴
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; // 如果arr1的值高于arr2,则返回值
}
}
}
return bReturn; // 已完成。返回结果。如果truearr1的值至少高于arr2的最低值。
}


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");
					}
					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.

解决方案

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


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


这篇关于比较两个数组的所有元素后,如何获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆