2017-03-16 92 views
0

有幾個問題。我創建了一個方法來搜索元素對象數組(每個元素對象已經用[atomicNumber縮寫名稱atomicWeight]初始化)。我還需要返回'對元素的引用' - 不完全確定如何執行此操作。用戶在main中輸入縮寫,然後在數組上使用findAbbreviation方法。 toString方法格式化並以String形式返回每個數據類型。我如何搜索整個數組的任何給定對象中的縮寫位置。我該如何返回對該「元素」對象的引用。如何搜索字符串數組中的特定單詞位置

public class PeriodicTable { 

    private final int MAX_ELEMENTS = 200; 
    private PeriodicElement[] table; 
    private int actualSize; 

    public PeriodicTable() throws IOException{ 
     table = new PeriodicElement[MAX_ELEMENTS]; 

     Scanner input = new Scanner(new File("file name here")); 
     int index = 0; 
     while(input.hasNext() && index < MAX_ELEMENTS) { 
      int aN = input.nextInt(); 
      String abbr = input.next(); 
      String name = input.next(); 
      double aW = input.nextDouble(); 
      table[index] = new PeriodicElement(aN, abbr, name, aW); 
      index++; 

     } 
     input.close(); 
     actualSize = index; 
    } 

    public String findAbbreviation(String abbreviationP){ 
     boolean found = false; 
     int index = 0; 
     while(found && index < MAX_ELEMENTS){ 
      if (table[index] = table[abbreviationP]){ 
       found = true; 

       return table[index].toString; 
      }    
      index++;  
     } 
     return null; 
    } 
} 

class PeriodicElement { 
    private int atomicNumber; 
    private String abbreviation, name; 
    private double atomicWeight; 

    public PeriodicElement(int atomicNumberP, 
      String abbreviationP, String nameP, 
      double atomicWeightP){ 
     atomicNumber = atomicNumberP; 
     abbreviation = abbreviationP; 
     name = nameP; 
     atomicWeight = atomicWeightP; 
    } 
+0

你是如何不斷進入這個循環?你的狀況開始是錯誤的。 –

+0

您也無法通過嘗試爲索引使用字符串來獲取數組中的元素。你從哪裏調用這個方法?您將不得不在循環中查找字符串並在搜索時跟蹤索引。如果你想索引縮寫P,那麼你需要使用for循環並搜索數組,直到找到它。在那一刻,你有了你找到的索引。 –

回答

0

首先,您需要一個數組或元素的集合。這可能是您正在編寫的類的實例變量,其中包括'findAbbreviation'。其次,一個「元素」可以簡單地有一個像「縮寫」這樣的屬性變量作爲Element類的一個實例變量,並且您可能只需要調用列表上的findAbbreviation並在該列表中專門搜索該縮寫。簡稱實例變量。您不可能搜索實際名稱以找到縮寫,例如:Gold的「縮寫」是AU。

您能否展示如何定義元素列表以及定義元素的類?

如果您只是通過元素的縮寫列表尋找(因爲當前的代碼提示),你可能只需要解決當前的代碼做一個正確的等於比較:

public String findAbbreviation(String abbreviationP){ 
     boolean found = false; 
     int index = 0; 
     while(!found && index < MAX_ELEMENTS){ //this should be !found instead of found 
      if (table[index].equals(abbreviationP)) { // you previously had an assignment statement in this if 
       found = true; 

       return table[index].toString; 
      }    
      index++;  
     } 
    return null; 
} 

更新答案反映問題的更新:

首先,您需要在PeriodicElement類中提供一個方法來獲取實例變量「abbreviation」。

這是一個標準的「吸」:

public String getAbbreviation() { 
    return abbreviation; 
} 

其次,你需要更新你的findAbbreviation方法來利用這一新的getter:

public PeriodicElement findAbbreviation(String abbreviationP){ 
     boolean found = false; 
     int index = 0; 
     while(!found && index < MAX_ELEMENTS){ //this should be !found instead of found 
      if (table[index].getAbbreviation().equals(abbreviationP)) { // you previously had an assignment statement in this if 
       found = true; 

       return table[index]; //This will return the object at the index where it was found. 
             // Notice I also changed the return type on your function. 
      }    
      index++;  
     } 
    return null; 
} 
+0

我已添加更多代碼。如果找不到元素,我將返回'null'。但是我打印出每個元素。是'返回null'; (因爲它是現在)在Java可以接受? – Tenze

+0

是的,返回空對象是可以接受的。由於你的返回類型是一個字符串,返回一個空字符串或「」更傳統一些。我已經更新了我以前答案下面的代碼。 – user681574

+0

這聽起來像你實際上想要返回PeriodicElement對象而不是String。如果您想提供元素的「字符串」版本,爲了獲得有意義的結果,您必須重寫Object類中的「toString」方法。 – user681574