简体   繁体   English

二分查找找到不重复java的元素

[英]binary search find the element that does not repeat java

//package alg;
import java.io.*;

//import java.io.File;
//import java.io.FileNotFoundException;
//import java.io.InvalidCommandException;
//import java.io.NumberFormatException;
import java.util.Arrays;
//import java.util.ArrayList;
//import java.util.List;
import java.util.Scanner;

//import javax.sound.sampled.Line;

public class FindOut
{
    public static void Find(int [] Ray, int min , int max)
    {


        if(min > max)
        {
            return;
        }

        if(min == max)
        {
            System.out.println(Ray[min]);
            return;

        }

        int med = (min + max)/2;


        if(med % 2 == 0)
        {

            if(Ray[med] == Ray[med + 1])
                Find(Ray, med + 2, max);

            else
                Find(Ray, min, med);

        }

        else //if(med % 2 == 1)
        //{

            if(Ray[med] == Ray[med-1])

                Find(Ray, med + 1 , max);

            else
                Find(Ray, min, med - 1);
    //  }



    }





    public static void main(String [] args) throws FileNotFoundException
    {

        @SuppressWarnings("resource")


        File file = new File(args [0]);

        Scanner scanner = new Scanner(file);


                try
        {

            int[] Ray = new int [5];    

            while(scanner.hasNext())
            {

                String num = scanner.next();



                 Ray = Arrays.stream(num.split(",")).mapToInt(Integer::parseInt).toArray();


                Find(Ray,0, Ray.length-1);





            }


                //  File inputFile = new File(num);


        }
        catch(NumberFormatException ex)
        {

        }

 }
}

What I need is to input a file through command line and it will read the integers inside place them into an array that I send through the function to find the element that appears only once such as我需要的是通过命令行输入一个文件,它会读取里面的整数,将它们放入我通过函数发送的数组中,以查找只出现一次的元素,例如

3 , 3 ,48, 48, 65, 95, 95 3、3、48、48、65、95、95

It should output它应该输出

65 65

But what it does is但它的作用是

3 3 48 48 65 95 95 3 3 48 48 65 95 95

I know its not the function I tested that with hard code and ran through the main logically step by step so can anyone explain the problem?我知道这不是我用硬代码测试的功能,并在逻辑上一步一步地运行了主要的功能,所以任何人都可以解释这个问题吗?

There is something wrong with how the file is read.文件的读取方式有问题。 It seems the code expects one line to contain the entire list of numbers.代码似乎希望一行包含整个数字列表。 However, Scanner uses whitespace as the default delimiter.但是, Scanner使用空格作为默认分隔符。 Find ends up being called once for every number in the file. Find最终会为文件中的每个数字调用一次。

To fix it, a delimiter can be specified like so:要修复它,可以像这样指定分隔符:

Scanner scanner = new Scanner(file).useDelimiter(",");

This means there is no need for the mapToInt thing you were doing.这意味着不需要你正在做的mapToInt事情。 Instead, I recommend you use a container from Collections such as ArrayList , and appending the number to it after conversion to an integer.相反,我建议您使用Collections的容器,例如ArrayList ,并在转换为整数后将数字附加到它。

public static void main(String[] args) throws FileNotFoundException {
    @SuppressWarnings("resource")
    File file = new File(args [0]);
    Scanner scanner = new Scanner(file).useDelimiter(",");

    try {
        ArrayList<Integer> Ray = new ArrayList<>();

        while (scanner.hasNext()) {
            String num = scanner.next().trim();
            Ray.add(Integer.parseInt(num));
        }

        scanner.close();
        int[] arr = Ray.stream().mapToInt(i -> i).toArray();
        Find(arr, 0, arr.length - 1);
    } catch (NumberFormatException ex) {

    }
}

Using an ArrayList allows it expand as needed, unlike an array, which has a fixed size.与具有固定大小的数组不同,使用ArrayList允许它根据需要扩展。 This is convenient because it means you don't have to know the amount of integers in the file ahead of time.这很方便,因为这意味着您不必提前知道文件中的整数数量。

To keep compatibility with your Find function, I converted the ArrayList back to a normal array.为了保持与您的Find函数的兼容性,我将ArrayList转换回普通数组。 However, I recommend you update your function to accept an ArrayList .但是,我建议您更新函数以接受ArrayList

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

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