我坚持这件事。我正在尝试将新信息添加到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;
}
}
我已经在'信息'中有一个字符串,但我想在'信息'中添加新信息(不能替换旧信息),所以当我去检查有关该人的所有信息时,将会
“旧”信息: blah blah blah
“新”信息:(如果我能把日期粘在这里也会很棒) Jada jada jada
我试过谷歌,但没有运气。我甚至不确定你能做到这一点......但我相信如果可以的话,这是值得一看的地方:)
感谢您的时间
答案 0 :(得分:1)
有点难以理解你想做什么 - 但我想你想修改Person类中的info字符串 - 这可以通过添加一个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,然后编辑他们的信息字段。