java如何从用户输入中删除2d数组中的行
如何从用户输入中删除二维数组中的行。我想用一个复制数组来覆盖原始的2d数组,但我似乎找不到语法来做这件事。另外,我希望复制数组覆盖原始数组,这样其他方法就不会受到影响
static String data[][] = new String[3][100];
int max = 0;
static void start() {
p2("---INVENTORY OPTIONS---");
p2("1. VIEW Inventory");
p2("2. ADD Product(Name, Price, Quantity)");
p2("3. UPDATE Product(Name, Price, Quantity)");
p2("4. DELETE Product(Name, Price, Quantity)");
p1("Enter inventory option: ");
String q2a = stringscan();
if (q2a.equals("1")) {
invview();
} else if (q2a.equals("2")) {
invadd(q2a);
} else if (q2a.equals("3")) {
invupdate(q2a);
} else if (q2a.equals("4")) {
invdelete(q2a);
}
}
static void invupdate(String q2a) {
if (q2a.equals("3")) {
p1("Input Product name to update: ");
String edit = stringscan();
for (int i = 1; i <= data.length; i++) {
if (data[0][i].contains(edit)) {
System.out.print("Edit or Retain Product Name [" + i + "]: ");
String qname = stringscan();
data[0][i] = qname;
System.out.print("Edit or Retain Product Price [" + i + "]: ");
String qprice = stringscan();
data[1][i] = qprice;
System.out.print("Edit or Retain Product Quantity [" + i + "]: ");
String qqty = stringscan();
data[2][i] = qqty;
}
}
invview();
start();
}
}
int[][] copy = new int[data.length - 1][data.length - 1];
static void invdelete(String q2a) {
if (q2a.equals("4")) {
p1("Input Product name to delete: ");
String edit = stringscan();
for (int i = 1, j = 1; i <= data.length; i++) {
if (!data[0][i].contains(edit)) {
///how to remove row from 2d array
}
}
invview();
start();
}
}
# 1 楼答案
要使此任务更简单、更灵活,您可能需要做的第一件事是创建一个产品类,例如:
当然,这意味着需要对代码的主要操作体进行更改,以利用产品的实例包含在List Inteface中的事实,从而更容易从实际创建的集合中添加和删除。使用收集机制(如列表)可以动态增大或减小其大小,而使用数组则需要处理固定大小的对象,并且几乎需要跳转(也就是说)来修改添加到或从中删除
利用上面提供的类和下面提供的代码,您将得到一个基本的可运行的产品清单控制台应用程序。当代码运行时,您可能会注意到的第一件事是主菜单稍微大一点。现在有七(7)个菜单项,而不是四(4)个菜单项。添加了加载库存、保存库存和退出等菜单项
菜单任务(退出除外)完成后,将始终显示主菜单。这当然允许应用程序连续运行,而无需重新运行。可以根据需要在库存中添加、更改或删除产品。库存中的产品表也可以随时查看。添加的产品也可以保存到本地文件系统中的产品文件中。该文件名目前硬编码为“Products.txt”,应根据用户的需要进行更改。像这样的硬编码从来都不是一个好主意,但在这里只是为了简单演示
在删除注释之前,请阅读代码中的所有注释: