我正在研究一个java程序,我在方法中定义并填充了几个向量(从文件中).我需要从方法中返回所有向量的内容.我听说你可以将它们全部放在一个物体中来归还它们.这有可能,如果是的话,怎么样?如果没有,你有什么可能的解决方案吗?在此先感谢您的帮助!
这是一段代码:
Object getInventory() { VectoritemID=new Vector (); Vector itemName=new Vector (); Vector pOrdered=new Vector (); Vector pInStore=new Vector (); Vector pSold=new Vector (); Vector manufPrice=new Vector (); Vector sellingPrice=new Vector (); Object inventoryItem=new Object(); //object to store vectors in try { Scanner infile= new Scanner(new FileReader("Ch10Ex16Data.txt")); int i=0; while (infile.hasNext()) { itemID.addElement(infile.next()); itemName.addElement(infile.next()+infile.nextLine()); pOrdered.addElement(infile.nextInt()); pInStore.addElement(pOrdered.elementAt(i)); pSold.addElement(0); manufPrice.addElement(infile.nextDouble()); sellingPrice.addElement(infile.nextDouble()); i++; } infile.close(); System.out.println(itemID); System.out.println(itemName); System.out.println(pOrdered); System.out.println(pInStore); System.out.println(pSold); System.out.println(manufPrice); System.out.println(sellingPrice); } catch (Exception f) { System.out.print(f); } return inventoryItem; }
Richard Walt.. 9
在个人情况下,我完全废弃了这种方法.您似乎需要Product类:
public class Product { private String itemName; private int itemID; // etc etc public Product(String itemName, int itemID) { this.itemName = itemName; this.itemID = itemID; // etc etc } public String getItemName() { return itemName; } public int getItemID() { return itemID; } // etc etc }
然后这样的事情:
public class Invertory { private Listproducts = new ArrayList // etc etc public Inventory(String fileName) throws IOException { // Load file, // Read each product, products.add(new Product(...product arguments); //add to array } public Product[] getProducts() { return products.toArray(new Product[]{}); }
}
在个人情况下,我完全废弃了这种方法.您似乎需要Product类:
public class Product { private String itemName; private int itemID; // etc etc public Product(String itemName, int itemID) { this.itemName = itemName; this.itemID = itemID; // etc etc } public String getItemName() { return itemName; } public int getItemID() { return itemID; } // etc etc }
然后这样的事情:
public class Invertory { private Listproducts = new ArrayList // etc etc public Inventory(String fileName) throws IOException { // Load file, // Read each product, products.add(new Product(...product arguments); //add to array } public Product[] getProducts() { return products.toArray(new Product[]{}); }
}