Java 从另一个类调用ArrayList

Java 从另一个类调用ArrayList,java,class,arraylist,calling-convention,Java,Class,Arraylist,Calling Convention,我在从另一个类调用arraylist时遇到问题。我在其中定义了一个名为IntBag的类和一个arraylist包。在main方法中,我想编写一个程序,使我能够更改其他类中arraylist的长度。当我尝试时,会出现“找不到符号”错误。你能帮忙吗 import java.util.*; public class IntBag { private static ArrayList<Integer> bag = new ArrayList<Integer>();

我在从另一个类调用arraylist时遇到问题。我在其中定义了一个名为IntBag的类和一个arraylist包。在main方法中,我想编写一个程序,使我能够更改其他类中arraylist的长度。当我尝试时,会出现“找不到符号”错误。你能帮忙吗

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); 
     }
}
import java.util.*;
公共类IntBag
{
私有静态ArrayList包=新建ArrayList();
私有整数maxNumber;
公共IntBag(ArrayList bag,int maxNumber)
{
这个袋子=袋子;
this.maxNumber=maxNumber;
}
公共静态数组列表getBag()
{
返回袋;
}
公共int sizeArray(ArrayList袋)
{    
返回袋。尺寸();
}
}
公开课考试
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
int选择,大小;
IntBag.getBag();
System.out.println(“输入数组的大小:”);
size=scan.nextInt();
bag=新阵列列表(尺寸);
}
}

IntBag是一个非静态类,这意味着要使用它,必须创建该类的新实例。为此,您可以执行以下操作:

IntBag name_of_object = new IntBag();
IntBag bag = new IntBag(new ArrayList<Integer>(), 10);
bag.setBag(new ArrayList<Integer>())
然后,要引用此对象内部的包,您可以通过调用:

name_of_object.getBag();
要从备用类更改ArrayList的大小,需要在IntBag类中包含setter方法:

public void setBag(ArrayList<Integer> newList) {
    this.bag = newList;
}
但请注意,ArrayList没有最大或最小大小。当您添加或删除变量时,它们会扩展或减少

将代码放在哪里?

好好想想。在主类中,您已经创建了一个对象,例如扫描仪和两个int。只要以相同的方式创建IntBag对象,就可以在任何需要使用它的地方使用它。因此,您的主要方法可能如下所示:

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 
 }
publicstaticvoidmain(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
int选择,大小;
System.out.println(“输入数组的大小:”);
size=scan.nextInt();
ArrayList bag=new ArrayList();//ArrayList没有大小。它们会自动展开或缩小
IntBag IntBag=new IntBag(bag,30);//创建一个新的IntBag对象
}

我想要这样:当我输入输入(大小)时,程序将调整IntBag类中定义的arraylist包的大小。我找不到应该将对象放在哪里。“你能告诉我密码吗?”哈罗德·芬奇查看我对答案的补充。如果回答了您的问题,请使用箭头下方的复选框将其标记为正确。如果没有,请告诉我有什么问题,或者你的其他问题是什么。
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 
 }