-1

I have a problem with calling an arraylist from another class. I defined a class called IntBag and an arraylist bag in it. In the main method I want to write a program which enables me to change the length of the arraylist from the other class. When I try I get a "cannot find symbol" error. Could you help?

import java.util.*;
public class IntBag 
{
   private static ArrayList<Integer> bag = new ArrayList<Integer>(); 
   private int maxNumber;

   public IntBag(ArrayList bag, int maxNumber) 
   {
       this.bag = bag;
       this.maxNumber = maxNumber;  
   }

   public static ArrayList getBag()
   {
       return bag;
   }

   public int sizeArray(ArrayList bag)
   {    
    return bag.size();
   }
}
public class test
{

    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        int choice, size;
        IntBag.getBag();
        System.out.println("Enter the size of an array : ");
        size = scan.nextInt();
        bag = new ArrayList<Integer>(size); 
     }
}
0

1 Answer 1

1

IntBag is a non-static class, which means to use it, you must create a new instance of that class. To do that, you would do something like the following:

IntBag name_of_object = new IntBag();

And then, to reference the bag inside of this object, you would access it by calling:

name_of_object.getBag();

To change the size of the ArraryList from an alternate class, you would need to include a setter method in your IntBag class:

public void setBag(ArrayList<Integer> newList) {
    this.bag = newList;
}

Then, in your alternate class you could do the following:

IntBag bag = new IntBag(new ArrayList<Integer>(), 10);
bag.setBag(new ArrayList<Integer>())

You could also create a similar setter for the maxnumber variable:

public void setMaxNumber(int max) {
    this.maxNumber = max;
}

But do note that ArrayLists have no max or min size. They expand or decrease as you add or remove variables from them.

Where to put the code?

Well think about it. In your main class you are already creating object such as a Scanner, and two ints. You would simply create the IntBag object in the same fashion, where ever you need to use it. So your main method may look something like this:

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);
    int choice, size;

    System.out.println("Enter the size of an array : ");
    size = scan.nextInt();

    ArrayList<Integer> bag = new ArrayList<Integer>(); // arrraylists do not have a size. They automatically expand or decrease

    IntBag intBag = new IntBag(bag, 30); // creates a new IntBag object 
 }
Sign up to request clarification or add additional context in comments.

3 Comments

I want this : When I enter a input(size), the program will resize the arraylist bag which is defined in IntBag class.
I couldn't find out where should I put the object. Could you show me on code?
@harold_finch Check out the additions I made to the answer. If this answers your questions, please mark it correct by using the check box under the arrows. If not, let me know what is wrong or what your other questions are.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.