我正在开发一个程序,它会将随机生成的数字添加到数组中。我需要能够跟踪在生成重复数字之前生成的数量。我有一个接口和ArrayIntLog类。该类包含一个int []。
在运行我的程序之前我得到一个错误,该程序说"数组需要,找到了ArrayIntLog。如果我理解我做得对,ArrayIntLog将输入存储到数组日志中,但我不确定如何从我的TestLuck类访问该数组?我尝试过使用名称myLog [index]并只记录[index]但是这些也给我带来了错误。
这是我的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 :(得分:0)
如果您需要访问其他类的非公共变量,最好调用该变量的getter方法。
在ArrayIntLog类中添加: -
public int get(int index) {
return log[index];
}
并在主方法中将if条件更改为: -
if (newLog.get(index) == (newLog.get(index2)))
{
System.out.println("You had to generate " + index + "numbers get a match");
}
答案 1 :(得分: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]))
,我没有检查代码的逻辑。
希望这有帮助
答案 2 :(得分:0)
要从其他对象访问数组'log'的内容,'ArrayIntLog'类必须有一个公共方法,可以访问aray'log'。
例如:
将以下代码添加到'ArrayIntLog'类中,并添加“int getData(int index);”进入IntLogInterface接口。
public getData(int index)
{
return log(index);
}
然后,您可以从newLog对象的数组中获取数据,该对象是“ArrayIntLog”类的实例,如下所示。此示例访问数组的第三个元素。
thirdDataInArray = newlog.getData(3);