Java 当arraylist位于其他类中时,查找arraylist的长度
所以我试图找出arraylist的长度,但是arraylist存储在另一个类中,我怎样才能解决这个问题Java 当arraylist位于其他类中时,查找arraylist的长度,java,file,arraylist,menu,Java,File,Arraylist,Menu,所以我试图找出arraylist的长度,但是arraylist存储在另一个类中,我怎样才能解决这个问题 import java.util.Scanner; import java.util.*; import java.util.ArrayList; public class StarberksInterface { public static void main(String args[]) { Scanner console = new Scanner(System.in);
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
public class StarberksInterface
{
public static void main(String args[])
{
Scanner console = new Scanner(System.in);
store = new Store();
String str, sName1, sName2, name;
char c;
int n=0;
sName1 = "Callahan";
sName2 = "Lambton";
//This is the main menu that will be displayed first.
System.out.println(" MAIN MENU FOR MANAGEMENT SYSTEM");
System.out.println("===============================================");
System.out.println("1. CHOOSE STORE");
System.out.println("2. DISPLAY STORES");
System.out.println("3. LOAD STORE VIA FILE");
System.out.println("4. SAVE STORE TO FILE ");
System.out.println("5. EXIT PROGRAM");
System.out.println("===============================================");
while(n!=5)// Exits the program when 4 is pressed
{
System.out.print("\n Please enter option 1-4 to continue...: ");
n = Integer.parseInt(System.console().readLine());
// Reads user input and takes them to selected code.
if (n>5||n<1)
{
System.out.print("Invalid input, please try again...");
continue;
}
if (n==1)// Takes to option 1 or sub menu
{
str="y";
while(str.equals("y")||str.equals("Y"))
{
System.out.println("Enter a store name [Callahan or Lambton] ");
name = console.next();
if (sName1.equals(name)|| sName2.equals(name))
{
StarberksInterface.subMenu();
continue;
}
else
{
System.out.println("There is no store under this name. Please try again.");
}
}
}
if (n==2)// Gathers products in stores and displays the number of products
{
System.out.println(" Store data is being displayed.");
System.out.println("===============================");
System.out.println("Store: Callahan");
System.out.println(" Number of products: "+store.size());
}
}
}
import java.util.Scanner;
导入java.util.*;
导入java.util.ArrayList;
公共类StarberksInterface
{
公共静态void main(字符串参数[])
{
扫描仪控制台=新扫描仪(System.in);
store=新商店();
字符串str,sName1,sName2,name;
字符c;
int n=0;
sName1=“卡拉汉”;
sName2=“Lambton”;
//这是将首先显示的主菜单。
System.out.println(“管理系统主菜单”);
System.out.println(“======================================================================”);
System.out.println(“1.选择存储”);
System.out.println(“2.显示存储”);
System.out.println(“3.通过文件加载存储”);
System.out.println(“4.将存储保存到文件”);
System.out.println(“5.退出程序”);
System.out.println(“======================================================================”);
while(n!=5)//按下4时退出程序
{
System.out.print(“\n请输入选项1-4以继续…:”;
n=Integer.parseInt(System.console().readLine());
//读取用户输入并将其带到所选代码。
如果(n>5 | | n 10))
{
System.out.println(“\n此输入不正确。请在3到10个字符之间进行说明。\n”);
System.out.println(“使用不同的输入重试”);
System.out.println(“\n******************************************************\n”);
StarberksInterface.addItem();
}
System.out.print(“请输入美元价格:”;
price=console.nextDouble();
产品定价(价格);
如果(价格<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
StarberksInterface.addItem();
}
System.out.print(“请输入设置价格。$:”;
sUpPrice=console.nextDouble();
产品。设置供应(供应);
如果(供应量<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
StarberksInterface.addItem();
}
System.out.print(“请输入单位成本.$:”;
unitCost=console.nextDouble();
产品。设置单位成本(单位成本);
如果(单位成本<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
StarberksInterface.addItem();
}
System.out.print(“请输入库存成本。$:”;
inventoryCost=console.nextDouble();
产品成本(存货成本);
如果(库存成本<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
StarberksInterface.addItem();
}
系统输出打印(“请输入库存金额:”);
stock=console.nextInt();
产品.固定库存(库存);
如果(库存<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
StarberksInterface.addItem();
}
系统输出打印(“请输入产品需求:”);
demand=console.nextInt();
产品设定值(需求);
如果(需求<0)
{
System.out.println(“\n此输入不正确。请确保属性为正数\n”);
System.out.println(“由于输入错误,程序将重新启动”);
System.out.println(“\n******************************************************\n”);
StarberksInterface.addItem();
}
System.out.println(“\n******************************************************\n”);
系统输出打印(desc+“产品添加成功”);
System.out.println(“\n******************************************************\n”);
//将项目存储在数组中
//检查项目是否已在列表中
/*while(产品!=null)
{
if(product.equals(store.getProduct(desc)))
{
System.out.println(desc+“已是产品”);
System.out.println(“数据输入将重新启动”);
StarberksInterface.addItem();
}
}*/
存储。添加(产品);
}
最后,下面的代码位于一个名为Store.java的文件中,这就是arraylist所在的位置
import java.util.ArrayList;
public class Store{
// stores the product information in an array list
//allows for numerous products and each can be called in the Starberks Interface
public ArrayList <Product> ProductList = new ArrayList<Product> ();
public Store()
{
}
public void add(Product product)
{
// Adds the product and all details entered by user to the list.
ProductList.add(product);
}
public Product getProduct(String prodName) {
//
for (int i = 0; i < ProductList.size(); i++) {
//searches through list of products to find a specific name entered in
// from the Starberks Interface
if (ProductList.get(i).getName().equals(prodName)) {
return ProductList.get(i);
}
}
return null;
}
}
import java.util.ArrayList;
公共类商店{
//将产品信息存储在数组列表中
//允许多种产品,每种产品都可以在Starberks接口中调用
public ArrayList ProductList=new ArrayList();
公共商店()
{
}
公共无效添加(产品)
{
//将产品和用户输入的所有详细信息添加到列表中。
ProductList.add(产品);
}
公共产品getProduct(字符串prodName){
//
对于(int i=0;iimport java.util.ArrayList;
public class Store{
// stores the product information in an array list
//allows for numerous products and each can be called in the Starberks Interface
public ArrayList <Product> ProductList = new ArrayList<Product> ();
public Store()
{
}
public void add(Product product)
{
// Adds the product and all details entered by user to the list.
ProductList.add(product);
}
public Product getProduct(String prodName) {
//
for (int i = 0; i < ProductList.size(); i++) {
//searches through list of products to find a specific name entered in
// from the Starberks Interface
if (ProductList.get(i).getName().equals(prodName)) {
return ProductList.get(i);
}
}
return null;
}
}
/*your list reference has a size() method that you can call to get the size of list in store class and can access that via a getter */
public int getProductListSize(){
return ProductList.size();
}
store. getProductListSize();