gpt4 book ai didi

java - 将用户创建的对象存储在 arrayList 中多次迭代

转载 作者:行者123 更新时间:2023-12-01 11:18:46 26 4
gpt4 key购买 nike

我已经在互联网上搜索了我遇到的问题的答案,但似乎无法得到我正在寻找的答案,我有两个类,应用程序类和用户类,我提示用户输入要么是新用户,要么返回已在数组列表中的用户的结果,该数组列表被设置为保存用户对象。应用程序结束后,我希望数组列表继续容纳对象,以便在应用程序类中每次连续运行 main 方法时,我都可以引用 arrayList 进行交叉检查。

所以我的问题是,当 main 方法完成并再次运行它时,它是否会从头开始重新创建我的所有对象和 arrayList?

下面是我正在使用的两个类(class)。应用程序类第一,用户第二。

import java.util.ArrayList;
import java.util.Scanner;

public class Application{
static Scanner in = new Scanner(System.in);


public static void main(String[] args) {


//Creating admin user object that will be able to access everything
Users admin = new Users();
Users result = null;


//creating a new user that is not an admin
System.out.println("Are you a new user?");
String answer = null;
answer = in.nextLine();

if(answer.equalsIgnoreCase("YES") || answer.equalsIgnoreCase("Y")) {
result = admin.addNewUser();
result.addUsertoArrayList(result);
}else {
result.displayUsers(result.users);
}
}//End of Main Method.
}//End of Application Class

import java.util.ArrayList;

public class Users extends Application {

private String username;
private double biWeeklyIncome = 0;
private String password;
private String email;

// ArrayList to store all the objects of Type Users.
ArrayList<Users> users = new ArrayList<Users>();



// Default Constructor.
Users() {
}

// Constructor that takes a string as a name parameter.
public String name;

Users(String name) {
this.name = name;
}

// Setter Methods.

// User name
public void setUsername() {
System.out.println("Enter the username that you wish to go by:\n Ex. bigBoss45");
username = in.nextLine();
}

// Income
public void setBiWeeklyIncome() {
System.out.println("Enter your current bi-weekly income: \n Ex. 4500.00");

try {
biWeeklyIncome = Double.parseDouble(in.nextLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

// Password
public void setPassword() {
System.out.println("Enter the password that you wish to access your account with:\n Ex. bigBoss45");


password = in.nextLine();
}

// Email
public void setEmail() {
System.out.println("Enter a valid email address: \n Ex. bigBoss45@gmail.com");

email = in.nextLine();
}

// Getter Methods

// User name
public String getUsername() {
return username;
}

// Income
public double getBiWeeklyIncome() {
return biWeeklyIncome;
}

// Password
public String getPassword() {
return password;
}

// Email
public String getEmail() {
return email;
}

// Method to create a new user
public Users addNewUser() {
String name = null;

System.out.println("Enter the name of the new user\n Ex.John Smith");
name = in.nextLine();
// Creating the new
Users newUser = new Users(name);

// Setting the new users information
newUser.setUsername();
newUser.setPassword();
newUser.setBiWeeklyIncome();
newUser.setEmail();

//adding the new user to the users arrayList

displayUsers(users);

return newUser;
}// end of addNewUser method.

//Method that is going to add a new user to the array List.
public void addUsertoArrayList(Users nUser) {
users.add(nUser);
}

public void displayUsers(ArrayList<Users> users) {
// Printing out the user added to the array list for testing purposes.
for (Users user : users) {
System.out.println(user.getUsername());
}
}//End of displayUser method.


}

我是 Java 和所有面向对象的新手,因此非常感谢您的帮助,感谢您花时间查看我的代码!

最佳答案

每次运行像 java Application 这样的命令来运行程序时,您都会启动一个新的 java 进程。内存中的任何数据都不会从 Java 程序的任何先前执行中继承。

如果您想存储数据,使其在流程的多次执行中保持不变,您应该考虑将其外部化到文件或数据库等。

关于java - 将用户创建的对象存储在 arrayList 中多次迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31497105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com