【发布时间】:2019-05-22 16:39:56
【问题描述】:
问题是已经为我填充了 10000 个数组。将填充数组的数字范围将在 0 到 2500 之间。数组未排序。我的目标是通过一次线性搜索找到 1320 的存在,第二次线性搜索将检查数字 1320 出现了多少次。该数组是一个随机数组。
我尝试设置一个线性搜索来检查数组中的数字是否存在。我还尝试设置线性搜索来检查数组存在多少次。都没有用,这是我第一次使用数组,所以我不确定我是否正确地做它们
public static void main(String[] args) {
// Finish adding Code
boolean DoesItExist;
int iHowMany;
final int SIZE = 10000, ENTRY = 1320;
int[] NumArray = new int[SIZE];
OwenHeading.getHeader("Assignment 9: Arrays.");
fillTheArray(NumArray, SIZE);
System.out.println("DONE");
}
public static void fillTheArray(int[] iTempArray, int SIZE) {
final int RANGE = 2500;
Random rand = new Random();
for (int index = 0; index <= SIZE - 1; index++)
iTempArray[index] = rand.nextInt(RANGE);
}
public static boolean linearSearchOne(int[] iTempArray, int SIZE, int ENTRY) {
boolean TempDoesItExist;
return TempDoesItExist;
}
public static int linearSearchTwo(int[] iTempArray, int SIZE, int ENTRY) {
int HowManyExist;
return HowManyExist;
}
public static void programOutput(boolean TempDoesItExist, int TempHowMany) {
if (TempDoesItExist)
System.out.println("does");
// Cool, you found the number 1320
else
System.out.println("does not");
// Dang, you didn't find the number 1320
}
}
我不是在要求确切的答案,只是一些帮助让我朝着正确的方向前进。如果我从头开始,我觉得我可以更轻松地完成这个项目,但是我的老师希望我们使用他的入门项目。
【问题讨论】:
-
到目前为止你尝试过什么?我们无法告诉您卡在哪里,让我们猜测我们应该向您解释的数组......
-
对我来说,答案似乎微不足道,我相信我错过了一些东西,
if (array[i] == 1320) { count++; }然后检查count是否 > 0 -
请详细描述发生了什么或没有发生什么。 “都没有工作”对于 SO 来说是不够的,对你来说也不应该是足够的。另外,请确保您的代码可以编译和运行,最好是在其中一个在线编译器上。
标签: java arrays linear-search