在案例1中,我将搜索Employee No,输出将是Employe Name,Number,Salary等。接下来我将编辑薪水。如何替换ArrayList
中的值?如果我在案例1中编辑薪水,案例2中的薪水必须替换为案例1编辑薪资中输入的值
String EmpName[]=new String [d];
Integer EmpNo[]=new Integer[d];
int Salary[]=new int[d];
int r=0, number, payment;
char menu;
String display, name;
System.out.print("Enter Number of Records: ");
r = Integer.parseInt(reader.readLine());
for(i=0; i<r;i++){
System.out.print("\nEnter Employee Number: ");
EmpNo[i]=Integer.parseInt(reader.readLine());
System.out.print("Enter Employee Name: ");
EmpName[i]=reader.readLine();
System.out.print("Enter Salary: ");
Salary[i]=Integer.parseInt(reader.readLine());
}
do {
System.out.println("\n\nDisplay Menu");
System.out.println("1.Edit Salary\n2.Display Employee Record\n3.Salary\n4.Exit Program");
System.out.print("Enter your choice: ");
a=Integer.parseInt(reader.readLine());
switch(a){
case 1:
System.out.print("\nEnter Employee Number: ");
number = Integer.parseInt(reader.readLine());
int y = Arrays.asList(EmpNo).indexOf(number);
if(Arrays.asList(EmpNo).contains(number)){
System.out.println("\nEmployee Number: "+EmpNo[y]+" \nEmployee Name: "+EmpName[y]+" \nSalary: "+Salary[y]+"");
System.out.print("\nEnter New Salary!: ");
//How can I replace the value in the Salary and the value must replace in the Case 2 too.
}
else{
System.out.println("Employee Number not Found!!");
}
break;
case 2:
System.out.print("\nEnter Employee Number: ");
number = Integer.parseInt(reader.readLine());
int x = Arrays.asList(EmpNo).indexOf(number);
if(Arrays.asList(EmpNo).contains(number)){
if(Salary[x]<4999)
System.out.print(+decimal.format(Salary[x]-Salary[x]*0.2+Salary[x]*0.3)+);
else if(Salary[x]>5000 && Salary[x]<9999)
System.out.print(+decimal.format(Salary[x]-Salary[x]*0.10+Salary[x]*0.5)+);
else if(Salary[x]>10000 && Salary[x]<19999)
System.out.print(+decimal.format(Salary[x]-Salary[x]*0.15+Salary[x]*0.7)+);
else if(Salary[x]>20000)
System.out.print(+decimal.format(Salary[x]-Salary[x]*0.20+Salary[x]*0.9)+);
}
else{
System.out.println("Name not Found!!");
}
break;
答案 0 :(得分:1)
如果您使用相同的数组列表来存储和检索薪水,并且如果您修改它,其他情况将会注意到更改,因为数组列表在它们的范围内。因此,您只需要修改包含薪水的数组列表。
答案 1 :(得分:0)
答案 2 :(得分:0)
持续使用创建ArrayList效率不高,
int y = Arrays.asList(EmpNo).indexOf(number);
所以只创建一次
ArrayList al = Arrays.asList(EmpNo);
获取项目
emp = al.get (index);
设置
al.set (index, newEmp);
在你的情况下,创建一个包含empId,empname和salary
的类Emp
会更好。
然后你的List可以是一个Employees列表,而不是三个empId,empname和salary
数组ArrayList <Employee> al = new ArrayList <Employee> ();
Employee emp = new Employee (....);
al.add (emp);
答案 3 :(得分:0)
Arrays.asList()
创建一个由最初提供的数组支持的List。因此,如果您在数组/列表中进行更改,则更改将反映在另一方。所以直接修改数组。