【问题标题】:Find if a number is in an array, and if so, how many times does it appear查找一个数字是否在数组中,如果是,它出现了多少次
【发布时间】: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


【解决方案1】:

初始化你的布尔值和计数器

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;
       }
    }

编辑:在第一个循环中添加了返回语句,因为找到值后没有理由继续

【讨论】:

  • 找到正确的值后为什么还要继续第一个循环?
  • 是的,对不起,这是我的疏忽,我先把它们组合在一起,然后在我再次阅读问题后分开
【解决方案2】:

您可以通过这种方式修改两种线性搜索方法,这将起作用: 只需声明一个计数器并在每次找到您的 ENTRY 编号时递增它。

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;
}

【讨论】:

  • 我删除了我之前的评论,因为它看起来需要循环两次但是当找到正确的值时你至少不能退出第一个循环,整个方法可以写成for (int i = 0; i &lt; SIZE; i++) { if (iTempArray[i] == ENTRY) { return true } return false; }
  • @JoakimDanielson 是的,你是对的,但只是想让所有的事情变得清晰明了
【解决方案3】:

您似乎没有调用线性搜索方法。对于线性搜索,您可以执行以下操作:

found=false; 
howManyCounter=0; 
for (int x=0, x<array.length; x++){
    if (array[x]==numberYouWant){
       found=true;
       howManyCounter++;  
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多