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

时间:2012-09-11 01:43:56

标签: java arrays

我有方法searchSales(),它应该找到所有与给定销售数字相等的销售数字。应用程序要求用户使用键盘输入给定的销售数字并搜索它。如果找到从键盘输入的销售数字,则应用程序显示销售数字/数字,否则显示相应的消息。好吧,我有一个代码只显示相同销售数字的第一个索引,例如:数组有元素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);
        }
    }

5 个答案:

答案 0 :(得分: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");
    }

}

答案 1 :(得分:0)

删除显示break;的行(此行使您的代码退出循环),将每个值存储在数组中并在最后打印出来,或打印出来而不是将它们存储在{{ 1}}变量。

答案 2 :(得分:0)

使用List<Integer>收集匹配的索引。类似的东西:

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

答案 3 :(得分:0)

使用Arrays.asList,然后在List上使用indexOf / lastIndexOf。类似的东西:

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

答案 4 :(得分:0)

在你的情况下,你真的不需要在任何地方存储任何东西,这样的东西就足够了:

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是另一回事。在那里,你必须存储东西:

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