简体   繁体   English

Java - 搜索数组中等于给定数字的所有元素索引

[英]Java - searching all elements indexes in array equal to given number

I have method searchSales() which is supposed to find all sales figures which are equal to the given sales figure.The application asks the user to enter the given sales figure using the keyboard and searches for it. 我有方法searchSales(),它应该找到所有销售数字等于给定的销售数字。应用程序要求用户使用键盘输入给定的销售数字并搜索它。 If sales figure entered from the keyboard is found then the application displays sales figure/figures otherwise it displays an appropriate message. 如果找到从键盘输入的销售数字,则应用程序显示销售数字/数字,否则显示相应的消息。 Well, i have a code which displays only first index of equal sales figure, for example: array has elements 1,2,3,3,4,5 and I want to find all indexes of [array] = 3. How can I do this? 好吧,我有一个代码只显示相同销售数字的第一个索引,例如:数组有元素1,2,3,3,4,5我想找到[array] = 3的所有索引。我怎么能做这个?

public static void searchSales(int search[]){

        Scanner input = new Scanner(System.in);

        System.out.print("Enter sales figure you want to find: ");
        int target = input.nextInt();
        int index = -1;
        for (int i=0; i<search.length; i++){
            if (search[i] == target){
                index=i;
                break;
            }
        }
        if (index == -1){
            System.out.println("Sales figure not found");
        }
        else {
            System.out.printf("Sales figure found at branch %d",index+1);
        }
    }
 public static void searchSales(int search[]){

    Scanner input = new Scanner(System.in);
    System.out.print("Enter sales figure you want to find: ");
    int target = input.nextInt();
    int index = -1;
    for (int i=0; i<search.length; i++){
        if (search[i] == target){
            index=i;
            System.out.printf("Sales figure found at branch %d\n",index+1);

        }
    }
    if (index == -1){
        System.out.println("Sales figure not found");
    }

}

Remove the line that says break; 删除说break;的行break; (this line makes your code exit the loop), store each value in an array and print them out at the end, or print them out instead of storing them in the index variable. (这一行使你的代码退出循环),将每个值存储在一个数组中并在最后打印出来,或打印出来而不是将它们存储在index变量中。

Use a List<Integer> to collect your matching indices. 使用List<Integer>来收集匹配的索引。 Something like: 就像是:

    int target = input.nextInt();
    List<Integer> indices = new ArrayList<Integer>();
    for (int i=0; i<search.length; i++){
        if (search[i] == target){
            indices.add(i);
        }
    }
    if (indices.isEmpty()){
        System.out.println("Sales figure not found");
    }
    else {
        System.out.println("Sales figures found:  " + indices);
    }

Use Arrays.asList and then use indexOf/lastIndexOf on the List. 使用Arrays.asList,然后在List上使用indexOf / lastIndexOf。 Something like: 就像是:

ArrayList<Integer> sales = Arrays.asList(search)
int start = sales.indexOf(target);
int end = sales.lastIndexOf(target);
if(start==end){
    System.out.printf("Sales figure found at branch %d",start+1);
}
else{
    for(int i=start;i<=end-start;i++)
        if(sales.get(i)==target)
            System.out.printf("Sales figure found at branch %d",i+1);
}

In your case you don't really need to store anything anywhere, something like this should suffice: 在你的情况下,你真的不需要在任何地方存储任何东西,这样的东西就足够了:

public static void searchSales(int search[]){
    Scanner input = new Scanner(System.in);
    System.out.print("Enter sales figure you want to find: ");
    int target = input.nextInt();

    boolean found = false
    for (int i = 0 ; i < search.length ; i++)
        if (search[i] == target) {
            found = true;
            System.out.printf("Sales figure found at branch %d", i + 1);
        }
    if (! found)
        System.out.println("Sales figure not found");
}


highestSales is a different story. highestSales是一个不同的故事。 There, you do have to store stuff: 在那里,你必须存储东西:

 public static void highestSales(int[] nums) { List<Integer> list = new ArrayList<Integer>(); list.add(0); for (int i = 1 ; i < nums.length ; i++) { if (nums[i] > nums[list.get(0)]) { list.clear(); list.add(i); } else if (nums[i] == nums[list.get(0)]) list.add(i); } System.out.println("Highest sales figure " + nums[list.get(0)] + " found at these branches"); for (int i : list) System.out.println(i); } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM