简体   繁体   English

查找数字是否在数组中,如果存在,则出现多少次

[英]Find if a number is in an array, and if so, how many times does it appear

The problem is there is an array of 10000 that is already filled for me. 问题是已经为我填充了10000个数组。 The range of numbers that will fill the array will be between 0 and 2500. The array is unsorted. 填充数组的数字范围在0到2500之间。数组未排序。 My goal is to find the existence of 1320 through one linear search, and the second linear search will check how many times the number 1320 appears. 我的目标是通过一次线性搜索找到1320的存在,第二次线性搜索将检查数字1320出现了多少次。 The array is a random array. 该数组是一个随机数组。

I have tried setting up a linear search that will check whether the number in the array exists. 我尝试设置线性搜索,该搜索将检查数组中的数字是否存在。 I have also tried to set up the linear search that will check how many times an array exists. 我还尝试设置线性搜索,该搜索将检查数组存在多少次。 Neither worked, this is my first time working with arrays so I am not sure if I am even doing them correctly 都不起作用,这是我第一次使用数组,所以我不确定是否正确地执行了它们

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
}

} }

I am not asking for the exact answer, just some help that will get me going into the right direction. 我并没有要求确切的答案,只是需要一些帮助,以使我朝正确的方向前进。 I feel like I would be able to do this project easier if I started from scratch, but my teacher wants us to use his starter project. 我觉得如果我从头开始,我将能够更轻松地完成这个项目,但是我的老师希望我们使用他的入门项目。

Initialize your boolean and counter 初始化您的布尔值和计数器

Bool doesItExist = false;
Int iHowManyTimes = 0;

You can check for values in arrays in java in a linear fashion like this: 您可以像这样以线性方式检查Java数组中的值:

for (int number : NumArray) {
    if (anItemInArray == myValue) {
        doesItExist = true;
        return;
    }
}

afterwards do it all over again and increment your counter 然后再做一遍,增加您的计数器

    for (int number : NumArray) {
       if (number == ENTRY) {
          iHowMany += 1;
       }
    }

Edit: Return statement added to first loop, as there is no reason to continue after the value is found 编辑:返回语句添加到第一个循环,因为没有理由在找到值后继续

It looks like you didn't call the linear search methods. 看来您没有调用线性搜索方法。 For a linear search you can just do something like the following: 对于线性搜索,您可以执行以下操作:

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

you can modify two methods of linear search you have in this way and that is going to work : just declare a counter and increment it each time you find your ENTRY number. 您可以通过这种方式修改线性搜索的两种方法,这将起作用:只需声明一个计数器,并在每次找到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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到一个数字在Java数组中重复多少次 - How to find how many times a number is repeated in an array in java 如何在数组中查找单词,然后查找它在 java 的数组中出现的字符数? - How do I find word in an array and find how many characters later does it appear in the array in java? 计算数组中出现了多少对数字 - Count how many pairs of a number appear in an array 找出一个值在二维数组中出现的次数,并将该数字存储在另一个一维数组中 - Find how many times a value appears in a 2D array and store that number in another 1D array 如何查找特定名称重复了多少次? - How to find a particular name repeated how many number of times? 如何查找字符串对象在Java中重复多少次? - How to find how many times does a string object repeat in java? 当我在 BST 中搜索一个单词时,如何找到它出现了多少次 - How can I find how many times a word appear when I search for it in a BST 一个数字出现多少次 - How many times a number occurs 如何查找Java中的数组列表中特定元素出现的次数 - How to find the number of times a specific element occurs in an Array list in Java 您如何在数组/字符串中找到特定值以及在数组/字符串中出现了多少次 - How do you find a specific value in an array/string and how many times it occurs in the array/string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM