在 Java 中将用户输入的元素添加到 null ArrayList 中

发布于 2025-01-15 01:00:54 字数 877 浏览 2 评论 0原文

我解释一下,我试图让用户在固定大小为五个元素的列表中输入五个元素;为此,我已将这些元素初始化为 null。我的问题是我不知道如何针对这种特殊情况执行此操作。我已经尝试过,但这显然不是正确的解决方案。

我将此代码制作成两个不同的空构造函数方法,以便在具有熟悉的 main 方法的主类中使用。但这堂课是我最麻烦的一堂课。

顺便提一句!我希望用户添加元素,而不是我。

package HashArray;
import java.util.ArrayList;
import java.util.Scanner;
public class arrayListNull {
    public static void arrayList() {
         ArrayList<Integer> list = new ArrayList<Integer>();
         list.add(null);
         list.add(null);
         list.add(null);
         list.add(null);
         list.add(null);  
    }
    public static void inputUser() {
        Scanner scanner = new Scanner(System.in);
        for(int i=0 ; i < 5 ; i++){
            arrayList.list.add(scanner.nextInt());
        }
        System.out.println(arrayList.list);
        scanner.close();
    }
}```

I explain, I am trying to make the user enter five elements in a list with a fixed size of five elements; for this I have initialized these elements in null. My problem is that I don't know exactly how to do it for this particular case. I've tried this, but it's clearly not the right solution.

I made this code into two different empty constructor methods, to be used in a main class with the familiar main method. But this class is the one I have the most trouble with.

Btw! I want the user to add the elements, not me.

package HashArray;
import java.util.ArrayList;
import java.util.Scanner;
public class arrayListNull {
    public static void arrayList() {
         ArrayList<Integer> list = new ArrayList<Integer>();
         list.add(null);
         list.add(null);
         list.add(null);
         list.add(null);
         list.add(null);  
    }
    public static void inputUser() {
        Scanner scanner = new Scanner(System.in);
        for(int i=0 ; i < 5 ; i++){
            arrayList.list.add(scanner.nextInt());
        }
        System.out.println(arrayList.list);
        scanner.close();
    }
}```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2025-01-22 01:00:54

您定义了一个名为 arrayList 的公共方法,该方法不返回任何内容 (void)。该方法 (a) 实例化一个 ArrayList 对象,(b) 使用 null 引用填充列表,这是多余的,因为 null 是默认值,(c) 丢弃该 ArrayList方法结束时的对象。噗,不见了。

因此,您的另一个方法 inputUser 引用了一个名为 arrayList 的变量,该变量从未存在过。

您的代码还有其他问题。您没有遵循 Java 命名约定:类以大写字母开头,方法和变量以小写字母开头。您过于依赖static,这可能表明您没有学习面向对象的概念。而且您似乎在盲目编码,没有经过深思熟虑,也没有使用其他示例代码。我建议您查看教科书和 Oracle 免费提供的 Java 教程

顺便说一句,使用 try-with-resources 语法可以自动关闭 Scanner 对象等资源。

You defined a public method named arrayList that returns nothing (void). That method (a) instantiates an ArrayList object, (b) populates the list with null references, which is redundant as nulls are the default, and (c) discards that ArrayList object by the end of the method. Poof, gone.

So your other method inputUser refers to a variable named arrayList which never existed.

Your code has other problems. You are not following Java naming conventions: classes start with uppercase, methods and variables lowercase. You are relying too much on static, likely a sign that you are not learning object-oriented concepts. And you seem to be coding blindly, without thinking it through, and without working with other example code. I suggest you review your textbook and the Java Tutorials provided by Oracle free-of-cost.

By the way, use try-with-resources syntax to automatically close resources such as Scanner objects.

寂寞美少年 2025-01-22 01:00:54

这很可能是你正在做的一项训练活动,而你被卡住了。你可以试试这个:

public static void inputUser() {
    List<Integer> arrayList = new ArrayList<>();
    int allowInputs = 5;

    Scanner scanner = new Scanner(System.in);
    for (int i = 0; i < allowInputs; i++) {
        arrayList.add(scanner.nextInt());
    }
    scanner.close();

    System.out.println("Your specified elements:");
    arrayList.forEach(System.out::println);
}

// RUN
public static void main(String[] args) {
    inputUser();
}

良好的编码! ˙_(ツ)_/˙

It is very likely that it is an activity of the training that you are doing and you are stuck. You can try this:

public static void inputUser() {
    List<Integer> arrayList = new ArrayList<>();
    int allowInputs = 5;

    Scanner scanner = new Scanner(System.in);
    for (int i = 0; i < allowInputs; i++) {
        arrayList.add(scanner.nextInt());
    }
    scanner.close();

    System.out.println("Your specified elements:");
    arrayList.forEach(System.out::println);
}

// RUN
public static void main(String[] args) {
    inputUser();
}

good coding! ¯_(ツ)_/¯

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文