2014-10-06 86 views
-3

我是java新手,一直在試圖編寫如何搜索多維數組。我的代碼適用於找到的元素,但是當我輸入不匹配的元素時,它不打印任何內容。請告訴我我的代碼有什麼問題。數組元素搜索

import java.util.Scanner; 
public class ArraySearch { 
public static void main (String[] args){ 
    Scanner input = new Scanner(System.in); 

    //lets create the array 
    int [] [] arrayOfInts = {{1, 2,3,4}, {5,6,7,8},{9,10,11,12}}; 

    //create search variables 
    System.out.println("Enter the key number to search for in the array: "); 
    int key = input.nextInt(); 
    boolean foundIt; 

    //perform search using a for loop 
    for (int i = 0; i <arrayOfInts.length; i++){ 
     for (int j = 0; j <arrayOfInts[i].length; j++){ 
      if (arrayOfInts[i][j] == key) { 
       foundIt = true; 

       if (foundIt) { 
        System.out.println("found " + key + " at row " +i+ " column " +j); 

       } else { 
        System.out.println(key + "is not in the array"); 
       } 
      } 
     } 
    } 
} 
} 
+1

@АлександрГончаренко'boolean'不能爲'null'。它沒有初始化,但不是'null'。 – khelwood 2014-10-06 09:31:04

+0

另外..你可以打破循環,當你找到元素,無需遍歷所有 – 2014-10-06 09:33:07

+0

請嘗試使用正確的代碼風格(格式)。我喜歡[Google Java Style](https://google-styleguide.googlecode.com/svn/trunk/javaguide.html),但您也可以在Google上找到其他樣式。通過使用適當的樣式,您可以輕鬆檢測代碼中的很多錯誤。它也增加了一般的可讀性。 – brimborium 2014-10-06 09:33:16

回答

2

你應該初始化布爾爲假,因爲局部變量必須在使用前進行初始化:

boolean foundIt = false; 

否則,如果沒有找到鑰匙,foundIt會當你訪問它是未初始化在你的條件。

不初始化foundIt應該給你一個compliation錯誤(The local variable foundIt may not have been initialized),但是你有另一個隱藏這個錯誤的錯誤。您打印輸出的if語句應該在for循環之外。現在它處於找到匹配的條件之內,所以只有在找到匹配的情況下才會對其進行評估。

+2

Java中'boolean'的默認值是'false'。 – brimborium 2014-10-06 09:26:59

+0

@brimborium它仍然需要初始化以編譯代碼。 – khelwood 2014-10-06 09:27:30

+0

@brimborium這隻適用於班級成員。局部變量必須被初始化。 – Eran 2014-10-06 09:28:01

1

您的包圍是錯誤的。 if - else語句

if (foundIt) { 
    System.out.println("found " + key + " at row " +i+ " column " +j); 

} else 
     {System.out.println(key + "is not in the array"); 
} 

在for循環的檢查中。

if (arrayOfInts[i][j] == key) { 

您可能希望將其放在for循環中以顯示每個匹配的消息。但你應該只是把一個println消息if語句內的for循環

if (arrayOfInts[i][j] == key) { 
    System.out.println("found " + key + " at row " +i+ " column " +j); 

當從來沒有發現的鍵打印其他消息,但是這在年底完成。確保你在開始時初始化布爾值!

boolean foundIt = false; 
... 
//at the end 
if(!foundIt) { 
    System.out.println("found " + key + " at row " +i+ " column " +j); 
} 
+1

@Paul不,它只打印在for的if內。所以只有當在if語句中找到匹配時,foundIt被設置爲true。 – Juru 2014-10-06 09:33:50

+1

啊是的。通過代碼審查驅動器出錯了。對不起 – 2014-10-06 09:38:16

1

您可以將您的代碼更改爲以下內容。你的代碼有很多問題。你必須做出的{}正確的順序,如果你這樣做,你需要初始化foundIt

Scanner input = new Scanner(System.in); 
    //lets create the array 
    int[][] arrayOfInts = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; 
    //create search variables 
    System.out.println("Enter the key number to search for in the array: "); 
    int key = input.nextInt(); 
    boolean foundIt = false; 
    for (int i = 0; i < arrayOfInts.length; i++) { 
     for (int j = 0; j < arrayOfInts[i].length; j++) { 
      if (arrayOfInts[i][j] == key) { 
      System.out.println("found " + key + " at row " + i + " column " + j); 
      // if found it will change the foundIt to true 
      foundIt = true; 
      } 
     } 
    } 
    if (!foundIt) { 
     System.out.println(key + "is not in the array"); 
    } 
+0

爲什麼我們不能在數組中找到元素時中斷? – 2014-10-06 09:40:42

+1

@SaiAvinash是的,我們可以打破。但是如果有重複的元素會發生什麼?還需要找到它們? – 2014-10-06 09:41:21

+0

我不會中斷,它顯示行和列,並使每個輸出都是唯一的。如果你只是想知道它是否被發現,那麼你可以打破更快。但是這個實現更完整,因爲它顯示了所有匹配。 – Juru 2014-10-06 09:43:55

0
// perform search using a for loop 
    for (int i = 0; i < arrayOfInts.length; i++) { 
     for (int j = 0; j < arrayOfInts[i].length; j++) { 
      if (arrayOfInts[i][j] == key) { 
        System.out.println("found " + key + " at row " + i + " column " 
          + j); 
        return;   
      } 
     } 
    } 
    System.out.println(key + "is not in the array"); 

我只是說上查找所需元素的回報,剪切/ else分支粘貼到的結束循環。

+0

內部if子句不是必需的,因爲您將foundIt設置爲true – Marco 2014-10-06 09:35:55

0

你的System.out只是這裏面如果塊

if (arrayOfInts[i][j] == key) 

,所以如果你沒有找到的東西不打印

我會做這樣:

... 
for (int i = 0; i <arrayOfInts.length; i++){ 
    for (int j = 0; j <arrayOfInts[i].length; j++){ 
     if (arrayOfInts[i][j] == key) { 
      foundIt = true; 
      // Tell where you found it 
      System.out.println("found " + key + " at row " +i+ " column " +j); 
     } 
    } 
} 
// After all check whether you found something anytime 
if(!foundIt){ 
    System.out.println(key + "is not in the array"); 
} 
... 
0

這是因爲您的打印語句if(foundIt)... else塊位於if(arrayOfInts [i] [j] == key)塊內。這意味着如果找不到int,那麼如果檢查打印的位置,代碼就不會進入內部。 您可以將「找不到」移動到最後。 例如:

boolean foundIt = false; 
    // perform search using a for loop 
    for (int i = 0; i < arrayOfInts.length; i++) 
    { 
     for (int j = 0; j < arrayOfInts[i].length; j++) 
     { 
      if (arrayOfInts[i][j] == key) 
      { 
       foundIt = true; 
       System.out.println("found " + key + " at row " + i + " column " + j); 
      } 

     } 
    } 
    if (!foundIt) 
    { 
     System.out.println(key + "is not in the array"); 
    } 

不要忘了首先將foundIt初始化爲false。

0

我建議的解決方案是:

bool foundit=false; 

for (int i = 0; i <arrayOfInts.length; i++){ 
     for (int j = 0; j <arrayOfInts[i].length; j++){ 
      if (arrayOfInts[i][j] == key) 
      { 
      foundIt = true; 
      break; 
      System.out.println("found " + key + " at row " +i+ " column " +j); 
      } 
     } 
} 

if(!foundit) 
{ 
system.out.println("Key not found in the array.") 
} 
0

這工作:

import java.util.Scanner; 
public class ArraySearch { 
public static void main (String[] args){ 
    Scanner input = new Scanner(System.in); 

    //lets create the array 
    int [] [] arrayOfInts = {{1, 2,3,4}, {5,6,7,8},{9,10,11,12}}; 

    //create search variables 
    System.out.println("Enter the key number to search for in the array: "); 
    int key = input.nextInt(); 

    //perform search using a for loop 
    for (int i = 0; i <arrayOfInts.length; i++){ 
     for (int j = 0; j <arrayOfInts[i].length; j++){ 
      if (arrayOfInts[i][j] == key) { 
       System.out.println("found " + key + " at row " +i+ " column " +j); 
       return; 
      } 
     } 
    } 
    System.out.println(key + " is not in the array"); 
} 
} 

爲什麼: 命令在操作if (arrayOfInts[i][j] == key) {}只有當元素在陣列中運行,所以它是沒有必要使用您的boolean foundIt;。使用return結束執行類,因爲我們已經找到了我們想要的東西。行System.out.println(key + " is not in the array");應該在兩個週期之後,所以它只有在我們檢查過二維數組的每個元素之後才能工作。