[英]How can I return different array lists types on java?
I have one method in which I create and add items to 3 array lists, 2 of them <String>
and the other one as <Integer>
. 我有一种方法可以在其中创建项目并将其添加到3个数组列表中,其中两个为<String>
,另一个为<Integer>
。 The problem is that I need to use them (all 3) in another method which will remove the item(s) that I want to remove from them. 问题是我需要在另一种方法中使用它们(全部3种),这将删除我要从它们中删除的项目。
This is the "add" method: 这是“添加”方法:
public static void Hire () throws IOException{
String name;
String category,st_wage,st_id;
int wage,id;
BufferedReader ob_name = new BufferedReader (new InputStreamReader (System.in));
BufferedReader ob_category = new BufferedReader (new InputStreamReader (System.in));
BufferedReader ob_wage = new BufferedReader (new InputStreamReader (System.in));
BufferedReader ob_id = new BufferedReader (new InputStreamReader (System.in));
//name on name_list[id]
//cat. on cat_list[id]
//wage on wage_list[id]
System.out.println("Driver Name: ");
name = ob_name.readLine();
System.out.println("Driver License: ");
System.out.println("1 - Motorcycle");
System.out.println("2 - Van");
System.out.println("3 - Truck");
category = ob_category.readLine();
System.out.println("Wage: ");
st_wage = ob_wage.readLine();
wage = Integer.parseInt(st_wage);
System.out.println("Driver ID: ");
st_id = ob_id.readLine();
id = Integer.parseInt(st_id);
ArrayList<String> name_list = new ArrayList<String>();
name_list.add(id, name);
ArrayList<String> cat_list = new ArrayList<String>();
cat_list.add(id, category);
ArrayList<Integer> wage_list = new ArrayList<Integer>();
wage_list.add(id, wage);
Manage();
}
And this is the "remove" method: 这是“删除”方法:
public static void Fire (ArrayList<String> name_list, ArrayList<String> cat_list, ArrayList<String> wage_list) throws IOException{
int id;
BufferedReader ob_id = new BufferedReader (new InputStreamReader (System.in));
BufferedReader ob_op = new BufferedReader (new InputStreamReader (System.in));
//name on name_list[id]
//cat on cat_list[id]
//wage on wage_list[id]
System.out.println("Driver ID: ");
String st_id = ob_id.readLine();
id = Integer.parseInt(st_id);
String name = name_list.get(id);
System.out.println("Are You Sure to fire Driver "+name+"? (Y/N)");
char op = (char) ob_op.read();
if(op == 'Y' || op == 'y'){
name_list.remove(id);
cat_list.remove(id);
wage_list.remove(id);
}
Manage();
}
You will get Array Index out of bound exception when adding elements in list with own index. 在具有自己索引的列表中添加元素时,将获得数组索引超出范围的异常。 For your requirement all your need is a simple class 根据您的要求,您只需要一门简单的课
public class Driver {
public String id ;
public String name ;
public String category ;
public double wage ;
public Driver(String id, String name) {
this.id = id;
this.name =name;
}
// getter and setter methods
}
Create a new instance of Driver after getting all inputs from user and keep it in a Map
从用户获得所有输入后,创建一个新的Driver实例并将其保存在Map
// Key = driverId and value = DriverEntity
HashMap<String,Driver> map = new HashMap();
Further you can add or remove with driver Id. 此外,您可以使用驱动程序ID添加或删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.