这是第一个代码:
public class Person {
private int age;
private String name;
public Person(String name,int age){
this.age = age;
this.name = name;
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String toString(){
return name+","+age;
}
}
然后这是主要类,它将显示输出:
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class PersonDatabase {
public static void main(String[] args) {
ArrayList<Person> list= new ArrayList<>();
Person p = new Person("",0);
int choice =0;
String listing ="";
do{
choice=Integer.parseInt(JOptionPane.showInputDialog(null,"\nChoices:"+"\n[1]Add"+"\n[2]Edit"+"\n[3]Delete"+"\n[4]Search"+"\n[5]View"+"\n[6]Sort"+"\n[7]Exit"+"\nEnter Choice:"));
switch(choice){
case 1:
String name = JOptionPane.showInputDialog(null,"Enter Name:");
int age = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Age:"));
list.add(new Person(name,age));
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
if(!list.isEmpty()){
for(int i=0;i<list.size();i++){
listing+=list.toString();
JOptionPane.showMessageDialog(null,listing);
}
}else{
JOptionPane.showMessageDialog(null,"ERROR","", JOptionPane.WARNING_MESSAGE);
}
break;
case 6:
break;
case 7:
break;
}
}while(choice!=7);
}
}
我很抱歉,如果我不清楚我的问题,但我想使用ArrayList,其中我将ArrayList存储在Person类中,我想使用public String toString查看它,所以它将显示名称和年龄
答案 0 :(得分:1)
使用column1
_ _
"from n'th\product_table of period"
"to n'th\product_table of classes"
方法从列表中获取Person
对象,然后连接。
我猜,一旦你获得了所有人的信息,那么你想表现出来。所以从for循环中取出list.get(index)
。否则就像你现在一样把它放在循环中。
JOptionPane.showMessageDialog(null,listing);
您正在使用for(int i=0;i<list.size();i++){
listing += list.get(i).toString() + "\n"; // get the i Person instance from list and call toString()
}
JOptionPane.showMessageDialog(null,listing);
运算符进行String
连接。您还可以使用+
类附加列表中的所有Person信息。
答案 1 :(得分:1)
看起来问题与您的案例5:代码有关。
尝试
case 5:
if(!list.isEmpty()){
for(int i=0;i<list.size();i++){
listing+=list.get(i).toString();
JOptionPane.showMessageDialog(null,listing);
}
}else{
JOptionPane.showMessageDialog(null,"ERROR","",
JOptionPane.WARNING_MESSAGE);
}
break;
注意 list.get(i).toString()而不是 list.toString()
答案 2 :(得分:1)
您的代码似乎正在ArrayList中正确插入Person类。所以看来,如果你需要使用toString()查看ArrayList的内容,你只需要覆盖Person类中的方法toString()。因此,当您打印ArrayList时,它只会为每个ArrayList元素调用toString()方法。
答案 3 :(得分:1)
public static void main(String[] args) {
ArrayList<Person> list= new ArrayList<>();
int choice =0;
do{
choice=Integer.parseInt(JOptionPane.showInputDialog(null,"\nChoices:"+"\n[1]Add"+"\n[2]Edit"+"\n[3]Delete"+"\n[4]Search"+"\n[5]View"+"\n[6]Sort"+"\n[7]Exit"+"\nEnter Choice:"));
switch(choice){
case 1:
String name = JOptionPane.showInputDialog(null,"Enter Name:");
int age = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Age:"));
list.add(new Person(name,age));
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
if(!list.isEmpty()){
foreach(Person p : list){
StringBuilder message = new StringBuilder();
message.append(p.toString());
message.append("\n");
JOptionPane.showMessageDialog(null,message.toString());
}
}else{
JOptionPane.showMessageDialog(null,"ERROR","", JOptionPane.WARNING_MESSAGE);
}
break;
case 6:
break;
case 7:
break;
}
}while(choice!=7);
}