Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-11-30 05:12:32

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. This is the first time I have been introduced to ArrayLists and classes. 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.

How can I create a private ArrayList of values that can be inputted via scanner thats all within its own 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. 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. 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.

    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;
        }
    }
Questioner
UnSure
Viewed
0
rzwitserloot 2020-11-30 13:29:01
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)); 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. 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());
}