2016-03-30 7 views
-2

削除部分を削除した場合、そのコードが機能していますが、存在する場合はその部分のif(a[i]==searchKey)にエラーが表示されます:incompatible types; int cannot be inverted to boolean、idk何が問題なのですか?Java(配列)insert、find、delete

  int []a={77,99,44,55,22,88,11,0,66,33}; 
      int i; 
      int searchKey; 
      int nElems=10; 

      for(i=0;i<nElems;i++) 

       System.out.print(a[i]+" "); 
       System.out.println(""); 
      // -------------------------------------------------------------------------- 

      searchKey=66; 
      for(i=0;i<nElems;i++) 

       if(a[i]==searchKey) 
        break; 
       if(i==nElems) 
        System.out.println("Can't find "+searchKey); 
       else 
        System.out.println("Found "+searchKey); 
      //-------------------------------------------------------------------------- 
       // DELETE PART ! 
       searchKey=55; 
      for(i=0;i<nElems;i++) 

       if(a[i]=searchKey) 
       break; 
       for(int j=i;j<nElems;j++) 
       a[j]=a[j+1]; 
       nElems--; 

      for(i=0;i<nElems;i++) 

       System.out.print(a[i]+" "); 
       System.out.println(""); 
+0

に記載されています。 – user2004685

答えて

0

もし代わりbooleanの評価においてsearchKeyの値を返すsearchKeyの値とa[i]を割り当てるif(a[i]==searchKey)

2

if(a[i]=searchKey)if(a[i]=searchKey)を交換。

==を使用してください。これは `` == `と`だけでなく `= IF([I] == SEARCHKEY)`であるべきである

+0

ありがとうございました@Mena –

+0

@BasilShahinよろしくお願いします。 – Mena

0
int []a={77,99,44,55,22,88,11,0,66,33}; 
int i; 
int searchKey; 
int nElems=10; 

for(i=0;i<nElems;i++) 

     System.out.print(a[i]+" "); 
     System.out.println(""); 
// -------------------------------------------------------------------------- 

searchKey=66; 
for(i=0;i<nElems;i++) 

     if(a[i]==searchKey) 
      break; 
     if(i==nElems) 
      System.out.println("Can't find "+searchKey); 
     else 
      System.out.println("Found "+searchKey); 
//-------------------------------------------------------------------------- 
     // DELETE PART ! 
     searchKey=55; 
for(i=0;i<nElems;i++) 
    // replace assignment operator (=) with realational operator (==) 
     if(a[i]==searchKey) 
     break; 
    //if this runs for 9th time then the index will 10 (j+1=10) so it will through an exception 
    for(int j=i;j<nElems-1;j++) 
     a[j]=a[j+1]; 
     nElems--; 

    for(i=0;i<nElems;i++) 

     System.out.print(a[i]+" "); 
     System.out.println(""); 

二つの変更が行われ、それはコメント行

+0

ありがとうございます@ rishi0405 –

関連する問題