我对这件事很困惑。我正在尝试将新信息添加到 ArrayList 中的“旧”信息中。我尝试了几种不同的方法,但我无法让它留在那里。
import java.sql.Statement;
import java.util.*;
public class Head {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String lname, fname, y, m, d, x, dob, phone, place = null, info;
int choice;
Date today = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(today.getTime());
ArrayList<Person>plist = new ArrayList<>();
ArrayList<Faktura>flist = new ArrayList<>();
Person p1 = new Person("Shaerer", "Lucas", "200307302232", "0702-523219", "Stockholm", "Her we put some info");
Person p2 = new Person("Smith", "Jacob", "197609071464", "0761-617163", "Miami", "Some information about this person");
plist.add(p1);
plist.add(p2);
case 2:
System.out.println("Add info");
System.out.println("------------------------------------------------------");
System.out.println(String.format("%-15s%-15s", p.getLname(), p.getFname()));
System.out.println("------------------------------------------------------");
System.out.println(String.format("DateOfBirth: "+ p.getDob()));
System.out.println(String.format("Adress: "+ p.getPlace()));
System.out.println(String.format("Phone: "+ p.getPhone()));
System.out.println(String.format("Info: \n"+ p.getInfo()));
System.out.println("\n" + sqlDate);
System.out.println("Info: ");
info = scan.nextLine();
// plist.add(this);
// for(int i = 0;i<plist.size(); i++){
// List<Person>plist = Arrays.asList(plist);
// for (Person s : plist)
// plist.add(s);
break;
构造函数
public class Person {
private String lname, fname, dob, phone, place, info;
public Person(String lname, String fname, String dob, String phone, String place, String info){
this.lname = lname;
this.fname = fname;
this.dob = dob;
this.phone = phone;
this.place = place;
this.info = info;
}
public String getLname(){
return lname;
}
public String getFname(){
return fname;
}
public String getDob(){
return dob;
}
public String getPhone(){
return phone;
}
public String getPlace(){
return place;
}
public String getInfo(){
return info;
}
public String toOutputFormat(){
return this.fname + this.lname + this.dob + this.phone + this.place + this.info;
}
}
我在“Info”中已经有一个字符串,但我想向“Info”添加新信息(而不是替换旧信息),因此当我去检查有关该人的所有信息时,将会出现
“旧”信息: 哈哈哈哈哈哈
"new"信息:(如果我也能把日期贴在这里那就太棒了) 贾达贾达贾达
我尝试用谷歌搜索这个,但没有成功。我什至不确定你能做到这一点...但我确信如果你能做到的话,这就是你该看看的地方:)
感谢您的宝贵时间
最佳答案
理解你想要做什么有点困难 - 但我猜你想修改 Person 类中的信息字符串 - 这可以通过添加 setter 来实现:
public setInfo(String newInfo){
info = newInfo;
}
甚至添加:
public addInfo(String newInfo){
info += newInfo;
}
使用它看起来像:
编辑:
import java.util.*;
public class Head {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String lname, fname, y, m, d, x, dob, phone, place = null, info;
int choice;
ArrayList<Person>plist = new ArrayList<>();
ArrayList<Faktura>flist = new ArrayList<>();
Person p1 = new Person("Shaerer", "Lucas", "200307302232", "0702-523219", "Stockholm", "Her we put some info");
Person p2 = new Person("Smith", "Jacob", "197609071464", "0761-617163", "Miami", "Some information about this person");
plist.add(p1);
plist.add(p2);
p1.setInfo("SomeThing new and exciting");
p1.addInfo(" and exhilarating");
System.out.println(p1.getInfo());
}
因为我不知道你要做什么 - 我只是提供一个例子。 上面的代码会将您的两个人添加到 plist,然后编辑他们的信息字段。
关于java - 将用户输入添加到ArrayList java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33744291/