简体   繁体   English

如何将数组传递给另一个类以查找最小值?

[英]How do I pass an array to another class to find the minimum value?

I am trying to find the minimum value in an array by way of creating a new Java class. 我试图通过创建一个新的Java类在数组中找到最小值。 I am having trouble passing in the array and using it to find the minimum value of an array. 我在传递数组并使用它来找到数组的最小值时遇到麻烦。

I have a total of two separate arrays that I am trying to find the value of. 我总共有两个单独的数组,我试图找到它们的值。

Here is the first class I created: 这是我创建的第一个类:

import java.util.*;

public class mooseWeight
{
    public int main(String[] args)
    {
        int[] length1;
        int[] length2;

        length1 = new int[20];
        length2 = new int[50];

        //Length 1
        System.out.println("Array 1:");
        Random rnd = new Random();
        for(int i = 0; i<length1.length; i++)
        {
            length1[i] = rnd.nextInt(400) + 250;
            System.out.println(length1[i]);
            return length1[i];
        }

        System.out.println("-----------------------------------");

        //Length2
        System.out.println("Array 2:");
        Random rnd2 = new Random();
        for(int j = 0; j < length2.length; j++)
        {
            length2[j] = rnd2.nextInt(400) + 250;
            System.out.println(length2[j]);
            return length2[j];
        }
    }
}

This is currently set up to find the minimum value of length1[] (aka the first array). 当前设置它是为了找到length1 []的最小值(又名第一个数组)。

This is the class I am trying to pass the arrays into: 这是我试图将数组传递给的类:

public class minWeight
{
    public static int getArrayMin(int[] arr)
    {
        mooseWeight array1[] = new mooseWeight[];


        int minValue = array1[0];
        for (int i = 0; i < array1.length; i++) 
        {
            if (array1[i] < minValue) 
            {
                minValue = array1[i];
            }

            //return minValue;
        }
        return minValue;
        System.out.println(minValue);
    }
}

My current error: 我目前的错误:

1 error found:
File: D:\2016-2017\Fall2016\201_CSCE_Programming\Lab 7\minWeight.java  [line: 5]
Error: array dimension missing

Your mooseWeight class contains your main, so when you're creating array1 , you're trying to create an array containing your actual program. mooseWeight类包含您的main,因此在创建array1 ,您试图创建一个包含实际程序的数组。

mooseWeight array1[] = new mooseWeight[]; is creating an array of mooseWeight objects. 正在创建一个mooseWeight对象数组。

As you don't define a size, this will fail, but even if it didn't, it wouldn't work. 由于您没有定义大小,因此将失败,但是即使没有定义,也将无法正常工作。

I assume that you meant to create an array of int , then loop over it. 我假设您打算创建一个int数组,然后对其进行循环。

If you created an array of int , then tried to loop over it without adding anything, the loop won't do anything as there's nothing to iterate over. 如果您创建了一个int数组,然后尝试在不添加任何内容的情况下对其进行循环,则该循环将不会执行任何操作,因为没有任何要迭代的内容。

In your method, you pass in an array of int called arr , so you need to use that to loop over. 在您的方法中,您传入了一个称为arrint数组,因此需要使用该数组进行循环。

If you're trying to pass 2 arrays into the method, creating an array as part of your method still won't work. 如果您尝试将2个数组传递给方法,则创建数组作为方法的一部分仍然无法正常工作。

You need to modify your method declaration to accept a second array. 您需要修改方法声明以接受第二个数组。

Assuming you want to find the smallest value from both of these arrays, I've modified my code snippet to do just that. 假设您想从这两个数组中找到最小值,则我修改了代码片段以实现此目的。 If you need to process your second array differently, modify the loop which deals with secondArr . 如果您需要不同地处理第二个数组,请修改处理secondArr的循环。

As mentioned in comments, your mooseWeight class should be named MooseWeight to keep with correct Java naming conventions. 如评论中所述,您的mooseWeight类应命名为MooseWeight以保持正确的Java命名约定。

A class name is in PascalCase, with instances in camelCase. 类名称在PascalCase中,实例在camelCase中。

To call the method, use the following snippet. 要调用该方法,请使用以下代码段。

int[] ar1 = new int[20];
int[] ar2 = new int[50];
//Populate your arrays with values....

int minValue = MinWeight.getArrayMin(ar1, ar2);

Class: 类:

 public class MinWeight {
      public static int getArrayMin(int[] arr, int[] secondArr) {

        int minValue = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < minValue) 
            {
                minValue = arr[i];
            }
         }

        for (int i = 0; i < secondArr.length; i++) {
            if (secondArr[i] < minValue) 
            {
                minValue = secondArr[i];
            }
         }          
        System.out.println(minValue);
        return minValue;
      }
    }

As a final concern, in your code, you return minValue before you try to print the result, so the print instruction is always unreachable. 最后要考虑的是,在您的代码中,您尝试打印结果之前会返回minValue ,因此打印指令始终无法访问。

The error in minWeight not fom the array parameter, it is from this line: minWeight中的错误不是fom数组参数,而是来自以下行:

mooseWeight array1[] = new mooseWeight[];

You cannot allocate an array of unknown dimension in java. 您无法在Java中分配未知维的数组。 Also, your attempt to do so makes no sense. 另外,您尝试这样做没有任何意义。 Just delete the line above from your java method and change the name of the parameter to array1. 只需从Java方法中删除上面的行,并将参数名称更改为array1。

Get an IDE like netbeans or eclipse. 获取一个像netbeans或eclipse这样的IDE。

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

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