简体   繁体   English

如何在 class 中创建私有 ArrayList? (爪哇)

[英]How do I create a private ArrayList within a class? (Java)

I am taking the first course in Java and for some reason my professor gave out 3 chapters of new material this week with required homework.我正在学习 Java 的第一门课程,出于某种原因,我的教授本周给出了 3 章新材料的要求和作业。 This is the first time I have been introduced to ArrayLists and classes.这是我第一次接触到 ArrayList 和类。 I normally would like to spend my time reading up on these things but I dont have time due to homework / quizzes (I have already done about 300 google searches) so anyways here is my question.我通常想花时间阅读这些东西,但由于家庭作业/测验我没有时间(我已经完成了大约 300 次谷歌搜索)所以无论如何这是我的问题。

How can I create a private ArrayList of values that can be inputted via scanner thats all within its own class?如何创建可以通过扫描仪输入的私有 ArrayList 值,这些值都在它自己的 class 中? This is for a grocery list program where I am trying to make a list of the items the user enters along with their values.这是一个杂货清单程序,我试图在其中列出用户输入的项目及其值。 This by itself is easy but the addition of classes with private ArrayLists makes it difficult.这本身很容易,但是添加带有私有 ArrayLists 的类就很困难了。 Normally I would do this...通常我会这样做...

    Scanner console = new Scanner(System.in);
    ArrayList<String> groceryList = new ArrayList<>();
    groceryList.add(console.next());

But with the addition of needing to make the ArrayList private and place it in its own class makes it more tricky.但是,除了需要将 ArrayList 设为私有并将其放置在自己的 class 中之外,它变得更加棘手。 Here is what I have so far but I have tried about 30 different things but I am only going to paste one of my tries.这是我到目前为止所做的,但我已经尝试了大约 30 种不同的东西,但我只会粘贴我的尝试中的一种。

    public class YourGroceryList {
        Scanner console = new Scanner(System.in);
        private ArrayList<YourGroceryList> groceryList = new ArrayList<YourGroceryList>();

        groceryList.add(console.next());
    }

Ok I have now updated my program and it does in fact work.好的,我现在已经更新了我的程序,它确实有效。 Let me know what I can improve on for next time!让我知道我下次可以改进的地方!

public class test {
    private static final int maxListSize = 10;
    private static String Item = "";

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        YourGroceryList list = new YourGroceryList();

        System.out.println("Type \"stop\" when you are done entering in your list");
        answer(console);

        System.out.println(YourGroceryList.getGroceryList());
        }

        public static void answer(Scanner console){
            for(int i=0; i<maxListSize; i++) {
                Item = console.next();
                if(Item.equals("stop")){
                    return;
                }
                YourGroceryList.setGroceryList(Item);
            }
        }

}

    class YourGroceryList {
        private static ArrayList<String> groceryList = new ArrayList();

        public static ArrayList<String> getGroceryList(){
            return groceryList;
        }

        public static ArrayList<String> setGroceryList(String Item){
            groceryList.add(Item);

            return groceryList;
        }
    }
public class YourGroceryList {
  Scanner console = new Scanner(System.in);
  private ArrayList<YourGroceryList> groceryList = new ArrayList<YourGroceryList>();
}

That part is fine.那部分很好。 These are field declarations: They declare a field by stating their name (such as console ), the type (such as Scanner ), and a so called initializing expression ( new Scanner(System.in) );这些是字段声明:它们通过声明其名称(例如console )、类型(例如Scanner )和所谓的初始化表达式new Scanner(System.in) )来声明字段; the initial value of these fields is set to these expressions, and if you never change these fields, that's what they'll remain.这些字段的初始值设置为这些表达式,如果您从不更改这些字段,它们将保持不变。

groceryList.add(console.next());

This is not allowed here.这是不允许的。 A type ( class YourGroceryList ) can only contain inner types (you don't need these here), fields, and methods.一个类型( class YourGroceryList )只能包含内部类型(这里不需要这些)、字段和方法。 That's a statement, thus, not allowed.这是一个声明,因此,不允许。 You'd have to put it in a method:你必须把它放在一个方法中:

public void doSomething() {
  groceryList.add(console.next());
}

If you want this to run when you create a new list, put it in the constructor (usually a bad idea; constructors should initialize the basics and have no side effects, but if you want, okay):如果您希望在创建新列表时运行它,请将其放在构造函数中(通常是个坏主意;构造函数应该初始化基础并且没有副作用,但是如果您愿意,可以):

public YourGroceryList() {
  groceryList.add(console.next());
}

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

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