Java 数组奇点中的最小数

Java 数组奇点中的最小数,java,arrays,java-8,Java,Arrays,Java 8,我试图解决一个关于CodeWars的问题,这个问题让我找到数组中的最小数。这是我目前的代码: public class SmallestIntegerFinder { public int findSmallestInt(int[] args){ int smallest = args[0]; for(int i=1; i < args.length; ++i){ if(args[i] < args[0]){ smallest

我试图解决一个关于CodeWars的问题,这个问题让我找到数组中的最小数。这是我目前的代码:

public class SmallestIntegerFinder {
public int findSmallestInt(int[] args){
    int smallest = args[0];
    for(int i=1; i < args.length; ++i){
        if(args[i] < args[0]){
            smallest = args[i];
        }
    }
    return smallest;
 }
}
公共类SmallestIntegerFinder{
公共int findsmalletint(int[]args){
int=args[0];
对于(int i=1;i
为什么这样不行?但是,当我将if语句中的args[0]更改为变量minimable时,它就起作用了。这两者有什么区别?它们都指向参数[0],不是吗

public class SmallestIntegerFinder {
public int findSmallestInt(int[] args){
    int smallest = args[0];
    for(int i=1; i < args.length; ++i){
        if(args[i] < smallest){
            smallest = args[i];
        }
    }
    return smallest;
 }
}
公共类SmallestIntegerFinder{
公共int findsmalletint(int[]args){
int=args[0];
对于(int i=1;i

我使用的测试数组是:{78,56232,12,11,43}

因为
args[0]
是数组中的第一个元素,而
最小的
从第一个元素开始;它将更新为下一个最小值(考虑3,1,2;2小于3,但1小于2)。你也可以用like

public int findsmalestint(int[]args){
int=args[0];
对于(int i=1;i
这是因为通过执行
minimate=args[i]
您正在更新
最小的
变量中的最小值,而不是
参数[0]

在第一个解决方案中,您将数组中的每个值与
args[0]
进行比较,但如果发现任何
i
th元素的值较小,则更新
最小的
变量,而不是
args[0]

如果希望第一个代码段正常工作,应该更新
args[0]

以下是更正后的代码片段:

public class SmallestIntegerFinder {
    public int findSmallestInt(int[] args) {
        int smallest = args[0];
        for(int i=1; i < args.length; ++i) {
            if(args[i] < args[0]){
                args[0] = args[i];
            }
        }
        return args[0];
    }
}
公共类SmallestIntegerFinder{
公共int findsmalletint(int[]args){
int=args[0];
对于(int i=1;i
这不一样

  • 在第一种情况下,将最小值作为args[i],但再次与args[0]进行比较。 因此,比较的基本值永远不会更新

  • 在第二种情况下,基本值将更新为当前最小值,并进行比较 这样你就得到了总的最小值


  • 如果将参数[0]中的值赋值,则该值不是指向该内存位置的指针。更改最小值不会更改参数[0]。因此,在循环中,您总是与args[0]进行比较,而不是与迄今为止看到的最低值进行比较。

    问题在于您是在与args[0]进行比较。你们应该和较小的进行比较,这样若有比较小的更小的东西,那个么就是新的较小的:)

    这应该起作用:

    public static int findSmallestInt(int[] args) {
        int smallest = args[0];
        for (int i = 1; i < args.length; ++i) {
            if (args[i] < smallest ) {
                smallest = args[i];
            }
        }
        return smallest;
    }
    
    公共静态int findsmalestint(int[]args){
    int=args[0];
    对于(int i=1;i
    使用


    其他答案已经向您解释了代码(不)工作的原因:在比较
    args[i]
    中,始终与数组的第一个值进行比较

    一句话:如果您使用的是Java 8,那么有一种更具可读性的解决方案:

    import java.util.stream.IntStream;
    public class Test {
      public static void main(String[] args) {
        int[] array = {78,56,232,12,11,43};
        int min     = IntStream.of(array).min().getAsInt();
        System.out.println("Minimum: "+min);
      }
    }
    

    您正在做的是将
    args[0]
    的内容复制到
    arg[0]
    最小的
    不是相同的内存位置

    在第一个代码块中,如果
    i
    th值小于
    0
    th,则更新
    minimable
    的内容。更新
    最小值
    只会更新该变量中的值,
    args[0]

    在第二个代码块中,您将使用
    args[i]
    中的值比较并更新
    最小的


    您需要了解在编写类似
    int
    的内容时会发生什么。这将为该变量分配新的内存部分。运行代码
    int minimest=1
    1
    放入
    minimest

    如果您有两个不同的变量,那么您将有两个不同的内存位置,即使您写入
    最小=最大
    。所有这些操作都是将内容从
    最大的
    复制到
    最小的

    当复制的是对象而不是基元类型时,这可能会造成混淆。
    显然,对于原语,值(例如
    1
    )从一个变量复制到另一个变量。最后的例子是
    最小值=1
    最大值=1

    当涉及到物体时,它有点不同。在
    Object minimable=new Object()
    的情况下,
    minimable
    将不包含
    对象
    ,而是内存中存在
    对象
    的位置。如果您现在执行
    maximum=minimate
    ,您将复制位置,而不是实际对象


    如果您对此感兴趣,您可以从下面我最喜欢的答案之一中了解更多信息:

    其他答案已经告诉您为什么第一位代码不起作用,并提供了许多替代方案,但请允许我在您的代码发生故障的地方详细说明。我觉得这将有助于避免类似的错误

    这是您的原始代码:

    public class SmallestIntegerFinder {
        public int findSmallestInt(int[] args){
            int smallest = args[0];
            for(int i=1; i < args.length; ++i){
                if(args[i] < args[0]){
                    smallest = args[i];
                }
            }
            return smallest;
         }
     }
    
    公共类SmallestIntegerFinder{
    公共int findsmalletint(int[]args){
    int=args[0];
    对于(int i=1;ipublic static int findSmallestInt(int[] args){
        return IntStream.of(args)
                        .min()
                        .orElseThrow(IllegalArgumentException::new);
    }
    
    import java.util.stream.IntStream;
    public class Test {
      public static void main(String[] args) {
        int[] array = {78,56,232,12,11,43};
        int min     = IntStream.of(array).min().getAsInt();
        System.out.println("Minimum: "+min);
      }
    }
    
    public class SmallestIntegerFinder {
        public int findSmallestInt(int[] args){
            int smallest = args[0];
            for(int i=1; i < args.length; ++i){
                if(args[i] < args[0]){
                    smallest = args[i];
                }
            }
            return smallest;
         }
     }
    
    final List<Integer> myList = Arrays.asList(78,56,232,12,11,43);
    int smallest = myList.stream()
          .sorted()
          .findFirst()
          .get();
    System.out.println("Smallest number is "+smallest);