Java 查找一个数字是否在数组中,如果是,它会出现多少次
问题是有一个10000的数组已经为我填充了。填充数组的数字范围将在0到2500之间。数组未排序。我的目标是通过一次线性搜索发现1320的存在,第二次线性搜索将检查1320出现的次数。该数组是一个随机数组 我已经尝试设置一个线性搜索来检查数组中的数字是否存在。我还尝试设置线性搜索来检查数组存在的次数。两者都不起作用,这是我第一次使用数组,所以我不确定我是否正确使用了它们Java 查找一个数字是否在数组中,如果是,它会出现多少次,java,arrays,linear-search,Java,Arrays,Linear Search,问题是有一个10000的数组已经为我填充了。填充数组的数字范围将在0到2500之间。数组未排序。我的目标是通过一次线性搜索发现1320的存在,第二次线性搜索将检查1320出现的次数。该数组是一个随机数组 我已经尝试设置一个线性搜索来检查数组中的数字是否存在。我还尝试设置线性搜索来检查数组存在的次数。两者都不起作用,这是我第一次使用数组,所以我不确定我是否正确使用了它们 public static void main(String[] args) { // Finish adding Co
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
}
publicstaticvoidmain(字符串[]args){
//完成添加代码
布尔DoesItExist;
我有很多;
最终整型尺寸=10000,入口尺寸=1320;
int[]numaray=新int[大小];
getHeader(“赋值9:Arrays.”);
填充阵列(NumArray,尺寸);
系统输出打印项次(“完成”);
}
公共静态无效填充阵列(int[]iTempArray,int SIZE){
最终整数范围=2500;
Random rand=新的Random();
对于(int index=0;index,您似乎没有调用线性搜索方法。对于线性搜索,您可以执行以下操作:
found=false;
howManyCounter=0;
for (int x=0, x<array.length; x++){
if (array[x]==numberYouWant){
found=true;
howManyCounter++;
}
}
found=false;
howManyCounter=0;
对于(int x=0,x,您可以通过这种方式修改两种线性搜索方法,这将起作用:
只需声明一个计数器,并在每次找到条目号时递增它
public static boolean linearSearchOne(int[] iTempArray, int SIZE, int ENTRY) {
for (int i = 0; i < SIZE; i++) {
if (iTempArray[i] == ENTRY) {
return true;
}
}
return false
}
public static int linearSearchTwo(int[] iTempArray, int SIZE, int ENTRY) {
int HowManyExist;
for (int i = 0; i < SIZE; i++) {
if (iTempArray[i] == ENTRY) {
HowManyExist ++;
}
}
return HowManyExist;
}
public静态布尔线性搜索(int[]iTempArray,int SIZE,int ENTRY){
对于(int i=0;i
初始化布尔值和计数器
Bool doesItExist = false;
Int iHowManyTimes = 0;
您可以使用如下线性方式在java中检查数组中的值:
for (int number : NumArray) {
if (anItemInArray == myValue) {
doesItExist = true;
return;
}
}
然后再重新做一遍,增加你的计数器
for (int number : NumArray) {
if (number == ENTRY) {
iHowMany += 1;
}
}
Edit:Return语句添加到第一个循环中,因为在找到值后没有理由继续执行到目前为止您尝试了什么?我们不知道您遇到了什么问题,让我们猜测我们应该向您解释的数组是什么…对我来说,答案似乎很琐碎,我相信我遗漏了什么,if(array[I]==1320){count++;}
然后检查count
是否>0请详细说明发生了什么或没有发生什么。“两者都不起作用”这样做是不够的,对你来说也不应该足够。另外,请确保你的代码编译并运行,最好是在一个在线编译器上。我删除了我以前的评论,因为它看起来需要循环两次,但你不能在找到正确的值后至少退出第一个循环,整个方法可以写成(inti=0;i
@JoakimDanielson是的,你是对的,但只是想让所有的事情变得明朗和明确。在找到正确的值之后,为什么要继续第一个循环?是的,很抱歉我疏忽了,在我再次阅读问题后,我先将它们合并,然后分开