代码之家  ›  专栏  ›  技术社区  ›  James Chavez

二进制搜索查找不重复java的元素

  •  -4
  • James Chavez  · 技术社区  · 7 年前
    //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)
            {
    
            }
    
     }
    }
    

    它应该输出

    但它的作用是

    3. 48 48 65

    我知道这不是我用硬代码测试的功能,并在逻辑上一步一步地运行主代码,所以有人能解释这个问题吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   bgfvdu3w    7 年前

    读取文件的方式有问题。代码似乎希望一行包含整个数字列表。然而 Scanner Find 最后,文件中的每个数字都会被调用一次。

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

    这意味着不需要 mapToInt 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) {
    
        }
    }
    

    阵列列表 与具有固定大小的阵列不同,它允许根据需要进行扩展。这很方便,因为这意味着您不必提前知道文件中的整数数量。

    保持与您的 函数,我将 阵列列表 返回正常阵列。但是,我建议您更新函数以接受 .