java,怎么输出ArrayList中保存的所有对象啊?
importjava.util.*;publicclassL4_1{publicstaticvoidmain(String[]args){ArrayListaa1=new...
import java.util.*;
public class L4_1 {
public static void main(String [] args){
ArrayList aa1=new ArrayList();
System.out.println("大小为"+aa1.size());//aa1.size()有多少个对象
Xs xs1=new Xs("悟空",15,90);
Xs xs2=new Xs("八戒",13,70);
Xs xs3=new Xs("沙僧",14,75);
Xs xs4=new Xs("小白龙",20,80);
aa1.add(xs1);//add是一个添加的方法
aa1.add(xs2);//添加一个对象
aa1.add(xs3);
aa1.add(1,xs4);
System.out.println("大小为"+aa1.size());//aa1.size()有多少个对象
// aa1.remove(1);//删除1号对象
for(int i=0;i<aa1.size();i++){
Xs tv=(Xs)aa1.get(i);
System.out.println("第"+(i+1)+"个学生姓名是"+tv.getXingming());
System.out.println("第"+(i+1)+"个学生是"+aa1);
}
}
}
class Xs{
private String xingming;
private int xuehao;
private int chengji;
Xs(String xingming,int xuehao,int chengji){
this.xingming=xingming;
this.chengji=chengji;
this.xuehao=xuehao;
}
public String getXingming(){
return xingming;
}
public int getXuehao(){
return xuehao;
}
public int getChengji(){
return chengji;
} }
运行结果:
大小为0
大小为4
第1个学生姓名是悟空
第1个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
第2个学生姓名是小白龙
第2个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
第3个学生姓名是八戒
第3个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
第4个学生姓名是沙僧
第4个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
怎么方便的输出下面的东西啊:
悟空,15,90
八戒,13,70
沙僧,14,75
小白龙,20,80 展开
public class L4_1 {
public static void main(String [] args){
ArrayList aa1=new ArrayList();
System.out.println("大小为"+aa1.size());//aa1.size()有多少个对象
Xs xs1=new Xs("悟空",15,90);
Xs xs2=new Xs("八戒",13,70);
Xs xs3=new Xs("沙僧",14,75);
Xs xs4=new Xs("小白龙",20,80);
aa1.add(xs1);//add是一个添加的方法
aa1.add(xs2);//添加一个对象
aa1.add(xs3);
aa1.add(1,xs4);
System.out.println("大小为"+aa1.size());//aa1.size()有多少个对象
// aa1.remove(1);//删除1号对象
for(int i=0;i<aa1.size();i++){
Xs tv=(Xs)aa1.get(i);
System.out.println("第"+(i+1)+"个学生姓名是"+tv.getXingming());
System.out.println("第"+(i+1)+"个学生是"+aa1);
}
}
}
class Xs{
private String xingming;
private int xuehao;
private int chengji;
Xs(String xingming,int xuehao,int chengji){
this.xingming=xingming;
this.chengji=chengji;
this.xuehao=xuehao;
}
public String getXingming(){
return xingming;
}
public int getXuehao(){
return xuehao;
}
public int getChengji(){
return chengji;
} }
运行结果:
大小为0
大小为4
第1个学生姓名是悟空
第1个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
第2个学生姓名是小白龙
第2个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
第3个学生姓名是八戒
第3个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
第4个学生姓名是沙僧
第4个学生是[Xs@1db9742, Xs@106d69c, Xs@52e922, Xs@25154f]
怎么方便的输出下面的东西啊:
悟空,15,90
八戒,13,70
沙僧,14,75
小白龙,20,80 展开
展开全部
package sync;
import java.util.*;
public class L4_1 {
public static void main(String[] args) {
ArrayList<Xs> aa1 = new ArrayList<Xs>();
System.out.println("大小为" + aa1.size());
Xs xs1=new Xs("悟空",15,90);
Xs xs2 = new Xs("八戒", 13, 70);
Xs xs3 = new Xs("沙僧", 14, 75);
Xs xs4 = new Xs("小白龙", 20, 80);
aa1.add(xs1);// add是一个添加的方法
aa1.add(xs2);// 添加一个对象
aa1.add(xs3);
aa1.add(1, xs4);
System.out.println("大小为" + aa1.size());// aa1.size()有多少个对象//
//aa1.remove(1);// 删除1号对象
for(int i=0;i<aa1.size();i++){
System.out.println(aa1.get(i));
}
}
}
class Xs {
private String xingming;
private int xuehao;
private int chengji;
Xs(String xingming, int xuehao, int chengji) {
this.xingming = xingming;
this.chengji = chengji;
this.xuehao = xuehao;
}
public String getXingming() {
return xingming;
}
public int getXuehao() {
return xuehao;
}
public int getChengji() {
return chengji;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return xingming+","+xuehao+","+chengji;
}
}
ps:变量名最好用英文有明确的意思,看着太别扭了。
import java.util.*;
public class L4_1 {
public static void main(String[] args) {
ArrayList<Xs> aa1 = new ArrayList<Xs>();
System.out.println("大小为" + aa1.size());
Xs xs1=new Xs("悟空",15,90);
Xs xs2 = new Xs("八戒", 13, 70);
Xs xs3 = new Xs("沙僧", 14, 75);
Xs xs4 = new Xs("小白龙", 20, 80);
aa1.add(xs1);// add是一个添加的方法
aa1.add(xs2);// 添加一个对象
aa1.add(xs3);
aa1.add(1, xs4);
System.out.println("大小为" + aa1.size());// aa1.size()有多少个对象//
//aa1.remove(1);// 删除1号对象
for(int i=0;i<aa1.size();i++){
System.out.println(aa1.get(i));
}
}
}
class Xs {
private String xingming;
private int xuehao;
private int chengji;
Xs(String xingming, int xuehao, int chengji) {
this.xingming = xingming;
this.chengji = chengji;
this.xuehao = xuehao;
}
public String getXingming() {
return xingming;
}
public int getXuehao() {
return xuehao;
}
public int getChengji() {
return chengji;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return xingming+","+xuehao+","+chengji;
}
}
ps:变量名最好用英文有明确的意思,看着太别扭了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询