简体   繁体   中英

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.

Output Format:

In each line, output the number located in yth position of xth line. 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 .

But my guess is that your code was originally declaring arr like this:

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 .

Why? Because null is not a valid integer value.


How to check an element in array exists or not in 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. 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 .

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. And if you do that an == null test is valid.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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