Java:如何从输入列表中返回 int 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16413032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:53:10  来源:igfitidea点击:

Java: How to return int array from list of inputs?

javaarraystypes

提问by LearnIT

After watching through a lot of basic tutorials, I decided it's time to try making a few simple applications of my own to help me understand OOP and remember what I learned so far.

在看了很多基础教程之后,我决定是时候尝试自己制作一些简单的应用程序来帮助我理解 OOP 并记住我到目前为止学到的东西。

My attempt is to make an app with a main class, and 2 object classes.

我的尝试是制作一个包含一个主类和 2 个对象类的应用程序。

I have Main.java which just calls Performer.java and Calculations.java.

我有 Main.java,它只调用 Performer.java 和 Calculations.java。

For now I am working on my Performer.java object.

现在我正在处理我的 Performer.java 对象。

I want the Performer.java to get 5 integers from the user, and then I want to be able to return it as an array and run it through my Calculations.java which would perform some simple tasks like counting averages and totals.

我希望 Performer.java 从用户那里获取 5 个整数,然后我希望能够将它作为数组返回并通过我的 Calculations.java 运行它,这将执行一些简单的任务,如计算平均值和总数。

I set my method to public, and if I understand correctly this should allow me to access these variables from anywhere else in my app.

我将我的方法设置为 public,如果我理解正确,这应该允许我从我的应用程序的任何其他地方访问这些变量。

I have tried returning it as

我试过把它退回

return arrayList[] = {u1,u2,u3,u4,u5};

return arrayList[] = {u1,u2,u3,u4,u5};

and this gave me an error "Enum header expected instead"

这给了我一个错误“预期的枚举标题”

import java.util.Scanner;

public class Performer {

    public static int getUnit(){
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the first number: ");
        int u1 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u2 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u3 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u4 = scan.nextInt();

        System.out.println("Enter the first number: ");
        int u5 = scan.nextInt();

        return u1, u2, u3, u4, u5;  <--------Confusing Part
    }
}

My second attempt was to return them as separate variables but this tells me expected type is int. Which I thought it is since those variables scan.nextInt().

我的第二次尝试是将它们作为单独的变量返回,但这告诉我预期的类型是 int。我认为这是因为这些变量 scan.nextInt()。

What can I do to return an array that I can pass through my other objects?

我该怎么做才能返回一个可以通过其他对象传递的数组?

Thank you!

谢谢!

回答by Subhrajyoti Majumder

you can use Listor array.

您可以使用List或数组。

Try this -

试试这个 -

public static List<Integer> getUnit(){
    List<Integer> list = new ArrayList<>();
    System.out.println("Enter the first number: ");
    list.add(scan.nextInt());
    ...

    return list;
}

回答by Robert M.

change the return value of getUnit() to int[] and return the values as follows:

将 getUnit() 的返回值更改为 int[] 并返回值如下:

return new int[] {u1, u2, u3, u4};

if you want to return a List, you can set the return value to List<Integer>and return the values as follows:

如果要返回一个List,可以将返回值设置为List<Integer>,返回值如下:

return Arrays.asList(u1, u2, u3, u4);

Note that Arrays.asList returns an immutable List, so you can't add or remove values from it.

请注意, Arrays.asList 返回一个不可变的 List,因此您不能在其中添加或删除值。

回答by kryptokinght

As you are new to programming, first let me clear some of the errors in your code. This line here:

由于您是编程新手,首先让我清除您代码中的一些错误。这条线在这里:

 return u1, u2, u3, u4, u5;  <--------Confusing Part

where you have used the return statement, the return allows only one value to be passed at a time, so you cannot pass all u1,u2,u3,u4 at the same time.

如果您使用了 return 语句,则 return 一次只允许传递一个值,因此您不能同时传递所有 u1、u2、u3、u4。

The solution to this has been provided above by the other coders: either you can change the return type of your getUnit() method to an integer array and returning the reference of that array or you can change the return type of your getUnit() method to an integer list and return the reference of the list.

其他编码人员已在上面提供了解决方案:您可以将 getUnit() 方法的返回类型更改为整数数组并返回该数组的引用,或者您可以更改 getUnit() 方法的返回类型到一个整数列表并返回列表的引用。

Since you are new to coding and you don't know about ArrayList, here is a link to it: Java ArrayList class

由于您是编码新手并且不了解 ArrayList,这里是它的链接: Java ArrayList 类

回答by Zaheer Ahmed

replace your confusing part:

替换您令人困惑的部分:

return new int[]{u1, u2, u3, u4, u5};

and function signature:

和函数签名:

public static int[] getUnit()

回答by roger_that

Try this

试试这个

`public static int[] getUnit() {

`public static int[] getUnit() {

    int[] unit = new int[5];
    Scanner in = new Scanner(System.in);
    for (int i = 0; i < unit.length; i++) {
        System.out.println("Enter element number " + (i + 1) + ":");
        unit[i] = in.nextInt();
    }
    System.out.println("All unit entered");
    return unit;
}`

This is what you want i guess.

这就是你想要的我猜。

回答by Gian

Well, to start with, the return type of your function is int. You need it to be int[]at the very least to denote that you want to return an array of integers, not a single integer.

好吧,首先,您的函数的返回类型是int. 您int[]至少需要它来表示您想要返回一个整数数组,而不是一个整数。

Then, you need to figure out the array literal syntax in Java.

然后,您需要弄清楚 Java 中的数组字面量语法。

And then, you should consider whether the Java collections classes might be a better fit for what you're trying to do. Check out some of the examples around how you construct an ArrayList.

然后,您应该考虑 Java 集合类是否更适合您要执行的操作。查看有关如何构建ArrayList.

回答by Suresh Atta

you can do this also:

你也可以这样做:

public static int[] getUnit(){
        Scanner scan = new Scanner(System.in);


        System.out.println("Enter the first number: ");
        int u1 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u2 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u3 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u4 = scan.nextInt();
        System.out.println("Enter the first number: ");
        int u5 = scan.nextInt();
        int[] array ={ u1, u2, u3, u4, u5};

        return  array;
        }
        }