简体   繁体   English

如何检查数组中的元素是否存在于Java中?

[英]How to check an element in array exists or not in java?

Input Format: 输入格式:

The first line has an integer . 第一行有一个整数。 In each of the next lines there will be an integer denoting number of integers on that line and then there will be space-separated integers. 在接下来的每一行中,将有一个整数,表示该行上的整数数量,然后将有空格分隔的整数。 In the next line there will be an integer denoting number of queries. 在下一行中,将有一个表示查询数量的整数。 Each query will consist of two integers x and y. 每个查询将包含两个整数x和y。

Output Format: 输出格式:

In each line, output the number located in yth position of xth line. 在每行中,输出位于第x行y位置的数字。 If there is no such position, just print "ERROR!" 如果没有这样的位置,只需打印“错误!”

   public class Solution {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int x = in.nextInt();
    Integer arr[][] = new Integer[x][];
    arr[x][100] = null; 
     for(int i=0;i < x;i++){
            int y = in.nextInt(); 
         for(int j = 0;j<y;j++){
             arr[i][j] = in.nextInt();
         }       
        }
    int z = in.nextInt();
     for(int k=0;k<z;k++){
         int p = in.nextInt();
         int q = in.nextInt();   
         if(arr[p-1][q-1] ==  null){

             System.out.printf("%s\n","ERROR!");

         }
          else 
              System.out.printf("%d\n",arr[p-1][q-1]); 

     }

}

When I run this I'm getting error as 当我运行这个时,我得到了错误

 Solution.java:23: error: incomparable types: int and <null>
           if(arr[p-1][q-1] ==  null){

How do I avoid this error in future? 将来如何避免此错误?

I can't reproduce your problem. 我无法重现您的问题。 When I compile your code, I get an error that is solved by adding an import statement. 编译您的代码时,出现错误,可以通过添加导入语句来解决。 Then when the code is runnable, I get an ArrayIndexOutOfBoundsException . 然后,当代码可运行时,我得到ArrayIndexOutOfBoundsException

But my guess is that your code was originally declaring arr like this: 但是我的猜测是您的代码最初是这样声明arr

int arr[][] = new int[x][];

Then this 那这个

if (arr[p-1][q-1] == null) {

will give you that the compilation error that you reported. 会给您报告的编译错误。

The error message is telling you that you cannot compare arr[p-1][q-1] with null , because arr[p-1][q-1] evaluated to an int , and a primitive type (such an int ) is not comparable with null . 错误消息告诉您,您无法将arr[p-1][q-1]null ,因为arr[p-1][q-1]计算结果为int以及原始类型(例如int )与null不具有可比性。

Why? 为什么? Because null is not a valid integer value. 因为null不是有效的整数值。


How to check an element in array exists or not in java? 如何检查数组中的元素是否存在于Java中?

It depends. 这取决于。

For an array whose base type is a primitive type, there is no way to do it. 对于基本类型为基本类型的数组,无法执行此操作。 Every element in (for example) an int[] exists, and has a value that is an integer. (例如) int[]每个元素都存在,并且其值为整数。 There is simply no way for an element to "not exist" ... provided that the index for the element is in the range 0 to array.length - 1 . 只要元素的索引在0array.length - 1的范围内,就根本不可能“不存在”。

For an array whose base type is a reference type (eg Integer ), an element can have the value null , and you could use that to mean that no value exists. 对于基本类型为引用类型的数组(例如Integer ),元素可以具有null ,并且您可以使用该值表示不存在任何值。 And if you do that an == null test is valid. 如果执行此操作,则== null测试有效。

public static void main(String args[]) throws ArrayIndexOutOfBoundsException
{
   //Your code..
    int p = in.nextInt();
    int q = in.nextInt();
    try
    {
        int r = arr[p-1][q-1];
        System.out.printf("%d\n",arr[p-1][q-1]);
    }
    catch(ArrayIndexOutOfBoundsException e)
    {
        System.out.printf("%s\n","ERROR!");   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM