ArrayList 存入資料和迴圈遍歷列印輸出
阿新 • • 發佈:2018-12-25
public class demo3 {
public static void main(String[] args) {
List<Num> list = new ArrayList<Num>();
for(int i =0;i<10;i++){
Num n = new Num();
n.setNum(i);
list.add(n);
}
System.out.println("集合長度為"+list.size());
for(int j=0;j<list.size();j++){
System.out.println("每個實體物件值為"+list.get(j).getNum());
}
}
}
class Num{
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public static void main(String[] args) {
List<Num> list = new ArrayList<Num>();
for(int i =0;i<10;i++){
Num n = new Num();
n.setNum(i);
list.add(n);
}
System.out.println("集合長度為"+list.size());
for(int j=0;j<list.size();j++){
System.out.println("每個實體物件值為"+list.get(j).getNum());
}
}
}
class Num{
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
輸出結果為:
集合長度為10
每個實體物件值為0
每個實體物件值為1
每個實體物件值為2
每個實體物件值為3
每個實體物件值為4
每個實體物件值為5
每個實體物件值為6
每個實體物件值為7
每個實體物件值為8
每個實體物件值為9