2014-09-04 125 views
0

我正在研究一個將隨機生成的數字添加到數組的程序。我需要能夠跟蹤生成重複號碼之前生成的號碼數量。我有一個接口和和ArrayIntLog類。該類包含一個int []。如果我明白我所做的是正確的,ArrayIntLog將輸入存儲到數組日誌中,但是我不確定如何訪問該數組,但是我不知道如何訪問該數組。 ?從我的TestLuck類陣列我使用名稱myLog [指數],只是日誌[指數],但但那些給我的錯誤以及試圖訪問另一個類中的數組?

這裏是我TestLuck類:

package arrayintlog; 

import java.util.Random; 

public class TestLuck 
{ 

    public static void main(String[] args) 
    { 
     int cycles = 0; 
     String name = "myLog"; 
     int min = 1; 
     int max = 10000; 
     int duplicateCheck; 

     Random rand = new Random(); 
     int random = rand.nextInt(max - min + 1) + min; 

     ArrayIntLog newLog = new ArrayIntLog(name); 

     for (int index = 0; index < newLog.size(); index++) 
     { 
      newLog.insert(random); 
      for(int index2 = 0; index2 < newLog.size(); index2++) 
      { 
       if (newLog[index].contains(newLog[index2])) 
       { 
        System.out.println("You had to generate " + index + "numbers get a match"); 
       } 
      } 
     } 

    } 
} 

這是我的ArrayIntLog類:

package arrayintlog; 


public class ArrayIntLog implements IntLogInterface 
{ 
    protected String name; //name of the IntLog 
    protected int[] log; //array that holds the integers 
    protected int lastIndex = -1; 

    //==========================Constructor===================================== 
    public ArrayIntLog(String name, int maxSize) 
    { 
     log = new int[maxSize]; 
     this.name = name; 
    } 

    //==========================Constructor===================================== 
    public ArrayIntLog(String name) 
    { 
     log = new int[100]; 
     this.name = name; 
    } 

    //===========================Insert========================================= 
    public void insert(int element) 
    { 
     lastIndex++; 
     log[lastIndex] = element; 
    } 

    //===========================isFull========================================= 
    public boolean isFull() 
    { 
     if(lastIndex == (log.length - 1)) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    //============================Size========================================== 
    public int size() 
    { 
     return lastIndex + 1; 
    } 

    //===========================Contains======================================= 
    public boolean contains(int element) 
    { 
     int location = 0; 
     while (location <= lastIndex) 
     { 
      if (element == (log[location])) 
      { 
       return true; 
      } 
      else 
      { 
       location++; 
      } 
     } 
     return false; 
    } 

    /*=============================Clear======================================== 
    public void clear() 
    { 
     for (int index = 0; index <= lastIndex; index++) 
     { 
      log[index] = null; 
     } 
     lastIndex = -1; 
    }*/ 

    //=============================getName====================================== 
    public String getName() 
    { 
     return name; 
    } 

    public String toString() 
    { 
     String logString = "Log " + name +"/n/n"; 

     for (int index = 0; index <= lastIndex; index++) 
     { 
      logString = logString + (index+1) + ". " + 
        log[index] + "/n"; 
     } 
     return logString; 
    } 
} 

這裏是我的IntLogInterface:

package arrayintlog; 


public interface IntLogInterface 
{ 
    void insert(int element); 
    //precondition: IntLog is not full 
    //places element into the log 

    boolean isFull(); 
    //returns true if the IntLog is full 

    int size(); 
    //returns the number of elements in this IntLg 

    boolean contains(int element); 
    //return true if the IntLog contatains an element 

    //void clear(); 
    //makes this IntLog empty 

    String getName(); 
    //returns the name of this IntLog 

    String toString(); 
    //returns a formatted string representing this IntLog 
} 
+0

'log [index]'總是非法的,除非'log'是一個數組。 '[]'語法僅適用於數組,數據庫完全停止,'ArrayIntLog'不是數組。 – immibis 2014-09-04 04:28:55

+0

沒錯,但在我的ArrayIntLog類的構造函數中有一個數組。所以每次我做newLog.insert(隨機)時,我應該將數字添加到數組中,但我不知道如何訪問數組,以便在運行時檢查重複項。 – twjohns29 2014-09-04 04:33:25

回答

0

如果您需要訪問其他類的非公共變量,你最好打電話給該變量的getter方法。在main方法

public int get(int index) { 
     return log[index]; 
    } 

而改變,如果條件以下: - - :

在ArrayIntLog類添加此

if (newLog.get(index) == (newLog.get(index2))) 
       { 
        System.out.println("You had to generate " + index + "numbers get a match"); 
       } 
0

我沒編譯代碼,但我認爲錯誤在行中:if (newLog[index].contains(newLog[index2])) 您已經這樣定義類ArrayIntLog的對象:

ArrayIntLog newLog = new ArrayIntLog(name); 

但是你試圖通過調用:newLog[index]來訪問它,它在Java中是非法的。 你可能想在課堂上ArrayIntLog +的界面來定義的另一種方法,是這樣的:

public class ArrayIntLog { 

    ///...... the rest of the code: 

    public int get(int index) { 
    // you might want to check bound here as well, its your decision... 
    return log[index]; 
    } 
} 

這將會給你一個接取到陣列。順便說一句,即使在這種情況下,這將是一個錯誤,因爲你不能做類似if (newLog.get(index).contains(newLog.(index2)))。我假設你的意思是: if (newLog.contains(newLog[index2]))或許:if (newLog.get(index) == newLog[index2])),我沒有檢查代碼的邏輯。

順便說一句,只是一個建議,java編譯器會產生相當不錯的錯誤信息,你甚至應該有一個確切的代碼行,編譯失敗的地方。所以真正值得閱讀這些編譯錯誤並試圖理解它們。

希望這有助於

0

要訪問來自其他對象陣列「登錄」的內容中,「ArrayIntLog」類必須具有一個公共方法,其提供通向阿雷「登錄」。

例如:

添加以下代碼插入 'ArrayIntLog' 級,並添加 「INT的getData(INT指數);」進入接口IntLogInterface。

public getData(int index) 
{ 
    return log(index); 
} 

然後就可以從newLog對象的陣列是「ArrayIntLog」類的實例如下獲取數據。這個例子訪問數組的第三個元素。

thirdDataInArray = newlog.getData(3);