我在使用该类对象的arraylist从类中获取历史记录时遇到问题,因为每次调用方法时arraylist都会重新初始化 这是我的代码:
public class User_Interface2 {
ArrayList<Removable> re=new ArrayList<Removable>();
private final ArrayList<Internal> in=new ArrayList<Internal>();
ArrayList<External> ex=new ArrayList<External>();
public static void main(){
User_Interface2 q=new User_Interface2();
System.out.println("Choose from the below options");
System.out.println("1.Internal Storage");
System.out.println("2.Removable device");
System.out.println("3.External Drive");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
sc.nextLine();
System.out.println("Enter the name of the file");
String s=sc.nextLine();
System.out.println("Enter the size of the file in GB");
double d=sc.nextDouble();
q.saveData(a,s,d);
}
public void saveData(int a,String s,double d){
switch (a) {
case 1:
in.add(new Internal());
in.get(in.size()-1).nameoffile=s;
in.get(in.size()-1).size=d;
in.get(in.size()-1).capacity();
break;
case 2:
re.add(new Removable());
re.get(re.size()-1).nameoffile=s;
re.get(re.size()-1).size=d;
re.get(re.size()-1).capacity();
break;
case 3:
ex.add(new External());
ex.get(ex.size()-1).nameoffile=s;
ex.get(ex.size()-1).size=d;
ex.get(ex.size()-1).capacity();
break;
default:
System.out.println("No such storage option available");
break;
}
}
}
这是内部类:
public class Internal extends Storage {
static double capacity=1024;
String nameoffile;
public double size;
void capacity(){
capacity=capacity-size;
System.out.println("Remaining capacity: "+capacity+"GB");
}
void persistent_save(){}
}
每当从另一个类调用User_Interface2类的main方法并且数据存储在内部类中时,在退出此类之后,arraylist将自行清除,并且我无法看到存储数据的历史记录。
这是我调用User_Interface2类的方法:
void Laptop(){
User_Interface2 d;
System.out.println("Welcome to Your Laptop");
System.out.println("Choose from the below options");
System.out.println("1.Show details of the laptop");
System.out.println("2.Display the data stored in your laptop");
System.out.println("3.Save Data");
System.out.println("4.Charge your Laptop");
System.out.println("0 to exit");
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
sc.nextLine();
while(a!=0){
if(a==1)
{processor();
Ram();
MotherBoard();}
else if(a==2)
{d=new User_Interface2();
d.showData();}
else if(a==3)
{d=new User_Interface2();
d.main();}
else if(a==4)
{charge();}
else
{System.out.println("No such Option is available");}
System.out.println("Welcome to Your Laptop");
System.out.println("Choose from the below options");
System.out.println("1.Show details of the laptop");
System.out.println("2.Display the data stored in your laptop");
System.out.println("3.Save Data");
System.out.println("4.Charge your Laptop");
System.out.println("0 to exit");
//Scanner sc=new Scanner(System.in);
a=sc.nextInt();
sc.nextLine();
}
}
}
答案 0 :(得分:0)
快速回顾一下这个街区:
while(a!=0){
...
else if(a==2)
{d=new User_Interface2();
d.showData();}
else if(a==3)
{d=new User_Interface2();
d.main();}
}
您正在每次迭代时创建一个新实例。
在循环外创建d
并重用该实例。
d=new User_Interface2();
while(a!=0){
...
else if(a==2) {
d.showData();
} else if(a==3) {
d.main();
}
}