java java比较数组中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26490633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:00:48  来源:igfitidea点击:

java comparing values in arrays

javaarrayscompare

提问by Nghia

Wasn't sure whether to make a new thread or not. but i'll post it here. I figured my explanation was not very good.

不知道要不要新建一个线程。但我会把它贴在这里。我想我的解释不是很好。

i have a class below

我在下面有一堂课

lets say int[] = 1204, 1205

让我们说 int[] = 1204, 1205

public class Job {

 private  int[] serviceCode =;     

 public Job (int[] jobCode) {


serviceCode = jobCode;

}

}

  public  int[] getJobCode() {
    return serviceCode;
 }

and this is the main program

这是主程序

public class MainProgram { 

public static void main {



}

}

}

how do i put the values of the array into separate integers?

我如何将数组的值放入单独的整数中?

回答by Grice

You can't directly compare an int[]to an int. You need to access a value in the array and then compare.

您不能直接将 anint[]与 an进行比较int。您需要访问数组中的一个值,然后进行比较。

int[] myArray = {1, 2, 3};
int value = 1;
if (myArray[0] == value) {/*do something*/}

回答by Aditya Singh

You cannot compare an array with a variable that holds just one data.

您不能将数组与仅包含一个数据的变量进行比较。

You know, array is a data structure.

你知道,数组是一种数据结构。

You must loop through the array and then compare each value of the array with t.

您必须遍历数组,然后将数组的每个值与t.

for(int i = 0; i < h.length; i++) {

    if( t == h[i] ) // Or any other comparison operator
        /* Perform some action.*/;
}

回答by Imad

Your int[] his an array of integers while int tis just a integer variable. So comparison is not at all possible. As per your question's title java comparing values in arrayshere is an answer.

int[] h是一个整数数组,而int t只是一个整数变量。所以比较根本不可能。根据您的问题标题,java comparing values in arrays这里是一个答案。

Either you can check whether this value exist in an array in forloop.

您可以在for循环中检查该值是否存在于数组中。

int val = 1;
int[] valArray = {1,2,3,4,5};
for(int i = 0; i < valArray.length ; i++)
{
     if (valArray[i] == val)
       {// Matched
       }
     else
      { // Not matched
      }

}

回答by Amit Sharma

Array is contiguous memory location, in order to get the specific values from the each addressable unit you have to access that addressable location. Suppose you declare an array like this.

数组是连续的内存位置,为了从每个可寻址单元中获取特定值,您必须访问该可寻址位置。假设您像这样声明一个数组。

int[] array = {10,20,30};

This will do the memory allocation like this: ( lets 0thelement is at address 100and assuming int size is of 4 byte)

这将进行这样的内存分配:(让0th元素位于地址100并假设 int 大小为4 byte

index        0      1      2
         -----------------------
         |  10  |  20   |  30   |
         -----------------------
address:    100    104    108

Array arrayname is basically base address form where the array starts in this case it is 100, next element will be present at 104and 108.

数组array名称基本上是数组开始的基地址形式,在这种情况下100,下一个元素将出现在104108

Now lets say we have an integer variable : int temp=100. Now to compare array element with integer variable temp, we have to access the each element of arrayof type integer situated at specific index. You have to compare like following:

现在让我们说我们有一个整型变量:int temp=100。现在要将数组元素与整数变量进行比较temp,我们必须访问array位于特定索引处的整数类型的每个元素。您必须进行如下比较:

for(int i=0; i<array.length; i++){
    if(temp>array[i])  /*array[i] is accessing the value at index i"*/ 
           //do something
    else
          //do something.
}