我正在研究一個將隨機生成的數字添加到數組的程序。我需要能夠跟蹤生成重複號碼之前生成的號碼數量。我有一個接口和和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
}
'log [index]'總是非法的,除非'log'是一個數組。 '[]'語法僅適用於數組,數據庫完全停止,'ArrayIntLog'不是數組。 – immibis 2014-09-04 04:28:55
沒錯,但在我的ArrayIntLog類的構造函數中有一個數組。所以每次我做newLog.insert(隨機)時,我應該將數字添加到數組中,但我不知道如何訪問數組,以便在運行時檢查重複項。 – twjohns29 2014-09-04 04:33:25