访问另一个类中的数组?
我正在研究一个将随机生成的数字添加到数组的程序。我需要能够跟踪生成重复号码之前生成的号码数量。我有一个接口和和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
}
如果您需要访问其他类的非公共变量,你最好打电话给该变量的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");
}
我没编译代码,但我认为错误在行中: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编译器会产生相当不错的错误信息,你甚至应该有一个确切的代码行,编译失败的地方。所以真正值得阅读这些编译错误并试图理解它们。
希望这有助于
要访问来自其他对象阵列“登录”的内容中,“ArrayIntLog”类必须具有一个公共方法,其提供通向阿雷“登录”。
例如:
添加以下代码插入 'ArrayIntLog' 级,并添加 “INT的getData(INT指数);”进入接口IntLogInterface。
public getData(int index)
{
return log(index);
}
然后就可以从newLog对象的阵列是“ArrayIntLog”类的实例如下获取数据。这个例子访问数组的第三个元素。
thirdDataInArray = newlog.getData(3);
'log [index]'总是非法的,除非'log'是一个数组。 '[]'语法仅适用于数组,数据库完全停止,'ArrayIntLog'不是数组。 – immibis 2014-09-04 04:28:55
没错,但在我的ArrayIntLog类的构造函数中有一个数组。所以每次我做newLog.insert(随机)时,我应该将数字添加到数组中,但我不知道如何访问数组,以便在运行时检查重复项。 – twjohns29 2014-09-04 04:33:25